1

Hello fellow stacked overflowers..

I am cu rrently having an issue with PHP's sleep() method when used in a loop.

I want ability to pause() a long batch script at any given moment.

In my long batch script I will check for a flag located in memcached; and if set, activated will try and "pause" the script.

Please see below:

session_write_close(); //Apparent Session Induced Fix that Doesn't Work
do {
    $halted = $this->memcache->get("halted");
    if ($halted) {
        sleep(1);
    }
} while ($halted);

With the above code I am able to successfully 'pause' my script. Except for the fact that sleep() appears to block ALL subsequent requests to the server; when in the loop.

I know sleep() needs to block the current thread to do its thing. But I'm not sure as to why it is blocking all of the subsequent requests.

  1. Does anyone have any idea as to what might be happening?
  2. Is there any alternate methods to effectively 'pause' any PHP script mid way?
xRavisher
  • 838
  • 1
  • 10
  • 18
  • PHP Script where you are using sleep() should not be using session, otherwise it will block all other Requests. `session_write_close()` should be at the top of your script and then you should not write anything in session. – ascsoftw Sep 01 '19 at 08:54
  • Try usleep instead of sleep. From usleep I do not know such problems. – jspit Sep 01 '19 at 18:10
  • Sorry but, none of the above helped? :'( – xRavisher Sep 04 '19 at 07:18
  • Possible duplicate of [How can I stop PHP sleep() affecting my whole PHP code?](https://stackoverflow.com/questions/1617412/how-can-i-stop-php-sleep-affecting-my-whole-php-code) – Mike Doe Sep 04 '19 at 07:32

0 Answers0