Long running Python script returned no result using shell_exec in PHP

Viewed 361

I use shell_exec to run a Python script from command line, to test, the Python script is simply waiting for a certain time period and then return True:

def test(minutes):
   import time
   time.sleep(minutes*60)
   return True

As I've tested, if the time period is 30 minutes or 60 minutes, the result was successfully returned to PHP, however if I set it to 80 minutes, PHP received no result nor even error messages.

The max_execution_time in php.ini is 30000 seconds, which is long enough.

Environment: Windows Server 2016 Standard with IIS

PHP version: 7.3.4

3 Answers

According to this answer the longer you sleep the more it is probable that you encounter issues, even though 80 minutes does look that long.

If that turns out to be indeed causing your problem here, maybe you should consider as a workaround to split the sleeping time into several blocks (say 1 minute for example):

def test(minutes):
   import time
   for k in range(minutes):
       time.sleep(60)
   return True

Try these things,

  1. In your PHP script, Add "timeout 100m" which may increase python script execution timeout

$output = shell_exec("timeout 100m python3 longrun.py"); echo $output;

enter image description here

  1. In your Server Config file ( For Apache2 in Ubuntu, it is "/etc/apache2/apache.conf" ) change timeout to 6000, default is 300. 6000 means 6000 seconds which equals to 100 minutes

enter image description here

Check your max_input_time in php.ini

Related