One ajax call block other ajax call

后端 未结 1 1759
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 22:43

Once my page is loaded, I perform an Ajax call to a php script, which updates my server. However this script can sometimes take over a minute to complete, and while the scri

相关标签:
1条回答
  • adeno's comment above is correct.

    "in PHP only one script at a time can operate on the same session, so as to not overwrite session data etc. So when doing two ajax calls to PHP scripts within the same session, the second has to wait for the first to finish"

    to help speed things up you can write to and end a session early(session_write_close()) to release the session-lock and allow another script using the session to continue.

    note: you can still read from your $_SESSION variable after calling session_write_close but you may no longer write to it.

    you can find a good example of this here: PHP Session Locks – How to Prevent Blocking Requests

    example provided from the link above:

    <?php
    // start the session
    session_start();
    
    // I can read/write to session
    $_SESSION['latestRequestTime'] = time();
    
    // close the session
    session_write_close();
    
    // now do my long-running code.
    
    // still able to read from session, but not write
    $twitterId = $_SESSION['twitterId'];
    
    // dang Twitter can be slow, good thing my other Ajax calls
    // aren't waiting for this to complete
    $twitterFeed = fetchTwitterFeed($twitterId);
    
    echo json_encode($twitterFeed);
    ?>
    
    0 讨论(0)
提交回复
热议问题