Sessions in PHP - An Underwater Pebble in Asynchronous Requests

    A little background.

    I have a hobby project
    The algorithm is this: the user enters a search query, this query “on the fly” searches for torrents on third-party trackers (root, rutracker, tfile, etc.).
    For parallel searches, several ajax requests go simultaneously, which must be processed asynchronously.
    However, requests were executed synchronously. If some tracker did not give an answer for a long time, then the rest of the requests hung and waited for an answer from the suspended tracker. The total query execution time was the sum of all requests. Although, according to my plans, the total time should have been equal to the longest request.
    Long puzzled why so. Sinned on HTTP pipelining. But, the reason was much more commonplace. It's all about the sessions. The fact is that sessions in php are consistent and php will not allow another process to refer to an already occupied session.

    Poke and enjoy the result here . Look at the total time with and without sessions.

    Why is this bad:

    If the site is slow, then when you open several tabs of this site, the tabs will be loaded in turn.
    If your content is loaded with a lot of ajax requests, then they will also be executed in turn.

    How to treat?

    Changing session handlers won't help. in the comments corrected that this is not so.
    As an option - (this was already written on the hub) in a quick section of code, use the session, and then execute session_write_close ();

    Also popular now: