PHP中具有进程分叉的Rabbitmq队列
我有一个基于PHP标准类的简单队列工作程序。它使用RabbitMQ作为服务器。我有一个队列类用于初始化AMQP连接wirh RabbitMQ。使用以下代码,一切正常:PHP中具有进程分叉的Rabbitmq队列,php,rabbitmq,amqp,Php,Rabbitmq,Amqp,我有一个基于PHP标准类的简单队列工作程序。它使用RabbitMQ作为服务器。我有一个队列类用于初始化AMQP连接wirh RabbitMQ。使用以下代码,一切正常: $queue = new Queue('myQueue'); while($envelope = $queue->getEnvelope()) { $command = unserialize($envelope->getBody()); if ($command instanceof QueueCo
$queue = new Queue('myQueue');
while($envelope = $queue->getEnvelope()) {
$command = unserialize($envelope->getBody());
if ($command instanceof QueueCommand) {
try {
if ($command->execute()) {
$queue->ack($envelope->getDeliveryTag());
}
} catch (Exception $exc) {
// an error occurred so do some processing to deal with it
}
}
}
然而,我想分叉队列命令执行,但在这种情况下,队列会随着第一个命令一次又一次的执行而无休止。我无法确认RabbitMQ已通过$queue->ack()接收消息;我的分叉版本(为了测试而简化为只有一个子版本)如下所示:
$queue = new Queue('myQueue');
while($envelope = $queue->getEnvelope()) {
$command = unserialize($envelope->getBody());
if ($command instanceof QueueCommand) {
$pid = pcntl_fork();
if ($pid) {
//parent proces
//wait for child
pcntl_waitpid($pid, $status, WUNTRACED);
if($status > 0) {
// an error occurred so do some processing to deal with it
} else {
//remove Command from queue
$queue->ack($envelope->getDeliveryTag());
}
} else {
//child process
try {
if ($command->execute()) {
exit(0);
}
} catch (Exception $exc) {
exit(1);
}
}
}
}
任何帮助都将不胜感激……我终于解决了这个问题!我必须从子进程运行ack命令,它是这样工作的! 这是正确的代码:
$queue = new Queue('myQueue');
while($envelope = $queue->getEnvelope()) {
$command = unserialize($envelope->getBody());
if ($command instanceof QueueCommand) {
$pid = pcntl_fork();
if ($pid) {
//parent proces
//wit for child
pcntl_waitpid($pid, $status, WUNTRACED);
if($status > 0) {
// an error occurred so do some processing to deal with it
} else {
// sucess
}
} else {
//child process
try {
if ($command->execute()) {
$queue->ack($envelope->getDeliveryTag());
exit(0);
}
} catch (Exception $exc) {
exit(1);
}
}
}
}