user2708647
user2708647

Reputation: 436

PHP session_write_close does not have parallel requests execute at same time on hosting server

I am sending several requests (100 for example) with ajax. I would like these time consuming requests to be treated in parallel on server side (PHP). Therefore I am closing the session on PHP side before executing consuming time request.

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
ob_start();
ignore_user_abort(true);
session_write_close();
header("Content-Encoding: none");
header("Content-Length: 0");
header("Connection: close");
ob_end_flush();
flush();
// time consuming request
...

This works perfectly well in localhost, but on hosting server it acts as if I had not closed session: I just have 6 requests working at the same time. Do you know what could explain this difference of behaviour between localhost and hosting server? How I could have theses requests run in parallel on hosting server? Thanks!

Upvotes: 1

Views: 343

Answers (1)

DfKimera
DfKimera

Reputation: 2146

What is probably happening is that the server has a different session handler configured (if it's a shared hosting provider, they probably have in-memory session storage to avoid heavy and expensive disk I/O). That means that the behavior of session_write_close may not always be what you expect.

If you want to execute several jobs in parallel, I would advise you to use queues, such as Beanstalkd, and avoid using session storage in the requests. Your AJAX script would push the job details to the queue, and a worker script running in command-line will be pulling these jobs from the queue and processing them. For the worker, you can use something like Supervisord or a recurring cronjob.

Upvotes: 1

Related Questions