使用异步长轮询?

发布于 2024-11-06 07:12:50 字数 203 浏览 6 评论 0原文

我有一个基于 Zend 的应用程序,它使用长轮询。基本上,它会发出一个 HTTP POST 请求,该请求会阻止应用程序,直到它返回或在 20 秒后超时。

我需要发出第二个请求(当前是非并行的),不幸的是,如果第一个请求挂起,则在第二个请求执行之前最终会等待 20 秒(= 超时)。

使我的应用程序异步或至少执行非阻塞 HTTP 请求 I/O 的最佳方法是什么?

I have an Zend–based application that uses long polling. Basically it makes a HTTP POST request, which blocks the application until it either returns or times out after 20 seconds.

I have a need to make a second request (which is currently non-parallel), where unfortunately if the first request hangs, it ends up being 20 seconds (= timeout) before the second request executes.

What is the best way to make my application asynchronous, or at the very least do non-blocking HTTP request I/O?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2024-11-13 07:12:50

如果您的两个请求都使用会话(session_start() 调用),并且您没有在长轮询脚本中关闭会话,则在长轮询运行期间,使用同一会话的其他脚本将锁定该会话。因此,这些脚本必须等待(我认为它们挂在 session_start() 上,但不确定)关闭会话,默认情况下,这是在脚本末尾自动完成的。

因此,如果您在长轮询中不需要会话,请不要在您的情况下运行 20 秒的代码之前(即在长轮询中的主迭代之前)启动或关闭它(调用 session_write_close())。

希望这有帮助。

If your both requests use session (session_start() call) and you don't close the session in long polling script, then the session is locked for other scripts using the same session for all the time the long polling runs. These scripts must therefore wait (i think they hang on session_start(), but not sure) for closing the session, by default this is done automatically at the end of a script.

So if you don't need session in long polling, don't start it or close it (call session_write_close()) before the code that runs for 20s in your case (i.e. before main iteration in long polling).

Hope this helps.

浅笑依然 2024-11-13 07:12:50

嗯,也许你应该在你的问题中添加更多信息。
如果这两个请求不相关(即第二个请求不需要第一个请求完成),您可以执行多个查询,而无需等待第一个请求完成。当然,如果没有 Javascript,你就无法做到这一点。

例如,您可以在异步模式下使用 jQuery ajax 函数(默认情况下它是异步的)。您可以在 jQuery 中链接多个 ajax 调用,第二个调用不会等待第一个调用完成(但要小心 ajax 超时设置)。

Mmmh, maybe you should add some more information to your questions.
If the 2 requests aren't related (i.e. the second one doesn't need the first one to be finished) you can perform several queries without waiting for the first one to finish. But of course you cannot do it without some Javascript.

For example you could use jQuery ajax function in asynchronous mode (by default it's asynchronous). You can chain several ajax calls in jQuery the second one will not wait for the first one to be finished (but be careful with ajax timeout settings).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文