我希望有一个PHP脚本,从RabbitMQ队列中消费(使用PECL AMQP模块),然后分叉去做实际的工作。
我有代码@ https://gist.github.com/giggsey/6666e67bb0e090eeb5f0
但是当我运行它时,我得到:
11296 Key: USER.12392 ObjectLength: 74 Forked 11296 at 2013-03-19
14:16:22 11277 ack() PHP致命错误:Uncaught exception
在tmp/fork .php:10中出现" AMQPConnectionException ",提示" Connection reset by peer "
堆栈跟踪:
0 tmp/fork .php(10): AMQPQueue->consume(Array)
1 tmp/fork .php(102): test->run()
2 {main}在tmp/fork .php第10行抛出
致命错误:未捕获的异常'AMQPConnectionException'带有消息'Connection reset by peer'在tmp/fork .php第10行
AMQPConnectionException: Connection reset by peer在tmp/fork .php第10行
调用堆栈:
0.0006 665008 1. {main}() tmp/forking.php:0 0.0007 665456 2. test->run() tmp/forking.php:102 0.0359 670504 3. AMQPQueue->consume() tmp/forking.php:10
您在连接建立后进行fork,这意味着子进程继承了该连接,这样父进程和所有子进程最终使用相同的连接,如果任何子进程退出(这会自动关闭连接)或关闭连接,父进程和所有兄弟进程会突然发现它们不再有连接。
所以不用:
$connection->connect();
使用较少文档的内容:
$connection->pconnect();
这将提供一个持久连接,这样当分叉的子进程关闭连接时,父进程会自动重新打开它。