我现在面临的问题是,我不知道如何在PHP中实现非阻塞请求。在我的应用程序中,任何用户活动都会被存档,这个存档过程可能会花费一些很长的时间--长达5-10秒。因为这个用户界面(在JavaScript中)不再响应,因为它还依赖于对服务器的请求。因此,假设我向服务器发出了如下请求(使用ExtJS库,这在这里并不重要):
Ext.Ajax.request({
url:'/handlers/archive.php', // my handler which I want to make non-blocking
method:'POST',
params:{...} // some parameters submitted to the server
});
... here are other multiple request to the server
... they are now waiting for the completion of heavy archive.php procedure那么,如何让这个php程序真正的非阻塞呢?任何非阻塞过程的工作示例都将非常有帮助。
发布于 2015-03-29 21:38:54
如果您希望客户端获得快速响应并继续处理服务器端的数据,则必须为此实现某种机制
例如:
来自client的
您可以使用RabbitMq或任何其他队列系统/消息系统
发布于 2015-03-29 21:43:08
除非你的web服务器被设置为可以处理它,例如通过Apache's MPM Worker,否则在web应用程序中multi-threaded PHP确实是很棘手的。
在这种情况下,我认为最好的方法是对fork a curl command使用exec()
$req = "curl -X POST -H 'Content-Type: application/json'";
$req.= " -d '" . $payload . "' " . "'" . $url . "'";
$req .= " > /dev/null 2>&1 &";
exec($req);https://stackoverflow.com/questions/29328559
复制相似问题