Use asynchronous long polling?
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 sec开发者_运维问答ond request executes.
What is the best way to make my application asynchronous, or at the very least do non-blocking HTTP request I/O?
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.
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).
精彩评论