note 98844 modified in function.pcntl-fork by danbrown

From: Date: Sat, 10 Jul 2010 19:15:30 +0000
Subject: note 98844 modified in function.pcntl-fork by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170865@lists.php.net to get a copy of this message
When using fork to run multiple children processes on a single job queue using mysql, I used mysql_affected_rows() to prevent collisions between workers: First I find a "free" job: SELECT job_id FROM queue WHERE status="free" Then I update the queue: UPDATE queue SET worker_id={$worker_id} WHERE job_id={$job_id} Then I see if the row was changed <?php if(mysql_affected_rows() == 0) { //the row hasn't changed, so it must mean that another worker has claimed the job, so I go back to the "find a free job" query } else { //do the job } ?> --was-- When using fork to run multiple children processes on a single job queue using mysql, I used mysql_affected_rows() to prevent collisions between workers: First I find a "free" job: SELECT job_id FROM queue WHERE status="free" Then I update the queue: UPDATE queue SET worker_id={$worker_id} WHERE job_id={$job_id} Then I see if the row was changed if(mysql_affected_rows() == 0) { //the row hasn't changed, so it must mean that another worker has claimed the job, so I go back to the "find a free job" query } else { //do the job } http://php.net/manual/en/function.pcntl-fork.php

« previous php.notes (#170865) next »