您遇到此问题的原因是该作业由主进程保留。在你调用之后pcntl_fork()
,实际上有一个$worker
变量的副本,因此主进程对作业有一个锁定,而第二个作业在尝试删除它时说它不存在(或者在这种情况下它被另一个进程保留) . 下面的代码通过创建一个新的工作人员来处理它,然后释放主工作人员的工作,并尝试在第二个工作人员上拿起它。
# worker for the main process
$worker = new \Pheanstalk\Pheanstalk($host, $port, $timeout, $persistent);
$pid = -1;
# seek out new jobs
while ($job = $worker->watch('queue_caller')->reserve()) {
# make sure pcntl is installed & enabled
if (function_exists('pcntl_fork')) {
# create a child process
$pid = pcntl_fork();
}
if ($pid === -1) {
# code will run in single threaded mode
} elseif ($pid !== 0) {
# parent process
# release the job so it can be picked up by the fork
$worker->release($job);
# short wait (1/20000th second) to ensure the fork executes first
# adjust this value to what is appropriate for your environment
usleep(50);
# clear out zombie processes after they're completed
pcntl_waitpid(0, $pidStatus, WNOHANG);
# go back to looking for jobs
continue;
} else {
# child worker is needed, because it has to own the job to delete it
/** @var Pheanstalk $worker */
$worker = new \Pheanstalk\Pheanstalk($host, $port, $timeout, $persistent);
# only look for jobs for 1 second, in theory it should always find something
$job = $worker->watch('queue_caller')->reserve(1);
if (false === $job) {
# for some reason there is no job
# terminate the child process with an error code
exit(1);
}
}
/** Start your code **/
do_something();
/** End your code **/
# delete the job from the queue
$worker->delete($job);
# only terminate if it's the child process
if ($pid === 0) {
# terminate the child process with success code
exit(0);
}
}