我正在寻找一个从 RabbitMQ 队列消费(使用 PECL AMQP 模块)然后 fork 以完成实际工作的 PHP 脚本。
我有代码@https://gist.github.com/giggsey/6666e67bb0e090eeb5f0
但是当我运行它时,我得到:
11296 Key: USER.12392 ObjectLength: 74 Forked 11296 at 2013-03-19
14:16:22 11277 ack() PHP Fatal error: Uncaught exception
'AMQPConnectionException' with message 'Connection reset by peer' in tmp/forking.php:10
Stack trace:
0 tmp/forking.php(10): AMQPQueue->consume(Array)
1 tmp/forking.php(102): test->run()
2 {main} thrown in tmp/forking.php on line 10
Fatal error: Uncaught exception 'AMQPConnectionException' with message 'Connection reset by peer' in tmp/forking.php on line 10
AMQPConnectionException: Connection reset by peer in tmp/forking.php on line 10
Call Stack:
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,这意味着子继承连接,这样父和所有子最终使用相同的连接,如果任何子退出(自动关闭连接)或关闭连接, parent 和所有 sibling 突然发现他们不再有联系。
所以不要使用:
$connection->connect();
使用记录较少的:
$connection->pconnect();
这将提供持久连接,这样当 fork 的子节点关闭连接时,父节点会自动重新打开它。
关于PHP AMQP Consume() fork 做实际工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15502037/