Is it Possible to Perform a background PHP server task from an AJAX call that will not lockup your site?

I have a server function like this

function very_long_task($data) {}

This function is called using $.ajax() function clients-side.

Read More

The problem is that when my server-side function very_long_task() is executed the site is locked down. Meaning that if I tried to view another page of the website from a different tab or window, the website will not load until the very_long_task() function has completed.

Is there anyway to get around this either server-side or client-side?

UPDATED: 2015-11-3

The AJAX call is actually called many times because it is looping through all the elements in a list and performing an action on each of them. The very_long_task() function is then being called on each element.

For example, if there were a list of 20 elements then the very_long_task() function would be called 20 times. This does help a little bit in the overall responsiveness on that page but not on other pages.

UPDATED: 2015-11-3

Also this is built with WordPress so I can leverage some of their functions, but I have had no luck with wp_schedule_single_event since I need a return value.

https://codex.wordpress.org/Function_Reference/wp_schedule_single_event

UPDATED: 2015-11-3

Here is an updated view of my

function very_long_task($data) {

    session_write_close();

    // Very long task...

    return $data;

}

Related posts

2 comments

  1. You’ll want to call session_write_close() as soon as possible.

    This is because while one page has called session_start(), the session file will be locked until the page finishes execution, or until the session is closed.

    If this is not done, any page calling session_start() will wait for the lock to be lifted.

    UPDATE

    I think I know what’s going on:

    your browser limits the number of simultaneous connections to a server, typically somewhere between 2 and 10.

    If you’re making 20 asynchronous AJAX calls, and you open the Developer Console (F12 / control-shift-I), you’ll probably find that not all of them are executing simultaneously. This would certainly leave no room for additional connections.

    Note, that the session_write_close() is still necessary, otherwise the ajax calls will execute serially.

    SUGGESTION

    So, it is best to only make one AJAX call.
    If you want parallelism, you can fork child processes server-side.

    You probably won’t be able to use jQuery for this, because you’ll want to send data from the server and flush()-ing it as it becomes available (HTTP streaming).

    One solution I used in a WP importer plugin is not to use AJAX at all, but perform the long running operation, pushing out HTML and a <script> tag to update the UI.

  2. I’m not entirely sure what you mean by “locked down” but below are some things to try:

    Make sure that your AJAX is asynchronous

    $.ajax({
        url: '/start_very_long_task.php',
        async: true
    });
    

    Make sure your PHP accommodates the expected behavior

    // start_very_long_task.php
    
    function start_very_long_task()
    {
        ini_set('ignore_user_abort','on');
        ini_set('max_execution_time', 0)
        session_write_close();
    
        do_very_long_task();
    }
    
    function do_very_long_task()
    {
        // Very long task stuff
        // This can recursively call itself without making
        //   making multiple calls to session_write_close(), etc...
    }
    
    start_very_long_task();
    

Comments are closed.