Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6549319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:04:43+00:00 2026-05-25T12:04:43+00:00

I have several time consuming database queries to run. Each has been built to

  • 0

I have several time consuming database queries to run. Each has been built to be triggered from an option chosen on a web page. I thought I was being quite cunning by firing off the work via several AJAX requests.

I presumed that multiple requests would be split over multiple processes/threads meaning the work would be completed relatively quickly for the user.

However, the requests seem to be processed in serial, meaning that no speed benefit is felt by the user.

Worse still, AJAX requests to update the page also wait in line, meaning they fail to respond until the previous requests have all completed.

I have read that this may be caused by the PHP sessions being locked.

What is the usual approach for this kind of issue?

  • Is there a way to force AJAX requests to work asynchronously?
  • Can I stop PHP from locking the sessions?
  • Should I use a seperate process via cron to fire background workings?

Thanks!

NB This project has been built using the symfony framework.

AJAX uses jQuery

// Get the content
$.get('/ajax/itemInformation/slug/'+slug, function(data) {
  $('#modal-more-information').html(data);
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:04:44+00:00Added an answer on May 25, 2026 at 12:04 pm

    If you are using sessions at all during any of the given AJAX requests, they will effectively execute serially, in order of request. This is due to locking of the session data file at the operating system level. The key to getting those requests to be asynchronous is to close (or never start) the session as quickly as possible.

    You can use session_write_close (docs) to close the session as soon as possible. I like to use a couple of helper functions for this, the set_session_var function below will open the session, write the var, then close the session – in and out as quickly as possible. When the page loads, you can call session_start to get the $_SESSION variable populated, then immediately call session_write_close. From then on out, only use the set function below to write.

    The get function is completely optional, since you could simply refer to the $_SESSION global, but I like to use this because it provides for a default value and I can have one less ternary in the main body of the code.

    function get_session_var($key=false, $default=null) {
        if ($key == false || strlen($key) < 0)
            return false;
        if (isset($_SESSION[$key]))
            $ret = $_SESSION[$key];
        else
            $ret = $default;
        return $ret;
    }
    function set_session_var($key=false, $value=null) {
        if ($key == false || strlen($key) < 0)
            return false;
        session_start();
        if ($value === null)
            unset($_SESSION[$key]);
        else
            $_SESSION[$key] = $value;
        session_write_close();
    }
    

    Be aware that there are a whole new set of considerations once the AJAX requests are truly asynchronous. Now you have to watch out for race conditions (you have to be wary of one request setting a variable that can impact another request) – for you see, with the sessions closed, one request’s changes to $_SESSION will not be visible to another request until it rebuilds the values. You can help avoid this by “rebuilding” the $_SESSION variable immediately before a critical use:

    function rebuild_session() {
        session_start();
        session_write_close();
    }
    

    … but this is still susceptible to a race condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Flex application which has several 'pages' worth of content. Each time
I have a python program that performs several independent and time consuming processes. The
I have several items on memcached that should expire 24h after the creation time.
I have several servers hosted with GoGrid, and every time I reboot one of
I have several bookmarked, but only have so much time in a day to
We have several jobs that run concurrently that have to use the same config
I'm building a program that has several functions that need to read data from
We have a GUI of several frames that build their contents dynamically. Each frame
I have a whole bunch of web services each with several web methods. The
I have several methods (involving network operations) which take quite a long time. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.