apw235 Posted June 8, 2009 Share Posted June 8, 2009 Hey - Running these on a Linux server with PHP 5 and Python 2.5. My problem is this: I have a simple php form that executes a long (~3 minutes) Python script using the 'exec' function from php. The issue is that the browser, I think, 'times out' before the Python script is finished and therefore kills Python and displays no output from Python. After some intense googling, I haven't found an answer, but I found a potential solution that I really want to avoid (manually opening a process that runs in the background, and checking its status until it is complete, then refreshing the web page). I'd much rather find an easier way to do this, and thus I am asking for help either way. If my only option is to open a job and monitor its status, could someone help guide me through coding this? I've tried naïvely doing exec("/path/to/executable arg1 arg2 >> /path/to/logfile 2>&1 &") but it seems as though even this dies when the browser times out. Thanks! -Adrian Quote Link to comment Share on other sites More sharing options...
apw235 Posted June 8, 2009 Author Share Posted June 8, 2009 Anyone? Quote Link to comment Share on other sites More sharing options...
jpratt Posted June 8, 2009 Share Posted June 8, 2009 Some of this might be helpful: http://us3.php.net/set_time_limit There is a time limit you can adjust for running scripts on your server. Quote Link to comment Share on other sites More sharing options...
apw235 Posted June 8, 2009 Author Share Posted June 8, 2009 Hey jpratt -- Thanks for the suggestion, unfortunately I don't want to have to tinker with this property. Thanks anyways! -Adrian Quote Link to comment Share on other sites More sharing options...
jpratt Posted June 9, 2009 Share Posted June 9, 2009 This is a server setting. This overrides anything the htaccess file sets it to. If you do not set your script limit time higher your script that takes ~3minutes will never finish because the default is set to something like 30 seconds. try putting this before your call to the python script: ini_set('set_time_limit', 300); Place this at the top of the php file and it will temporarily set the limit for this file only for 5 minutes. Quote Link to comment Share on other sites More sharing options...
apw235 Posted June 9, 2009 Author Share Posted June 9, 2009 Hey - Taken from the PHP manual: Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. Another strange thing - say my python code looks like this: import sys import os thisPID = os.getpid() print thisPID RADeg = float(sys.argv[1]) decDeg = float(sys.argv[2]) clipSizeDeg = float(sys.argv[3]) ic.myFunction(RAdeg, decDeg, clipSizeDeg) os.system(SOME LONG COMMAND) for i in os.listdir(blah): os.system("gunzip BLAH") If I throw random print statements in there, say like this: import sys import os thisPID = os.getpid() print thisPID print "1" RADeg = float(sys.argv[1]) decDeg = float(sys.argv[2]) clipSizeDeg = float(sys.argv[3]) print "2" ic.myFunction(RAdeg, decDeg, clipSizeDeg) print "3" os.system(SOME LONG COMMAND) print "4" for i in os.listdir(blah): print i os.system("gunzip BLAH") It prints all of these numbers from the print statements, but doesn't execute the rest of the code - the stuff that I actually want it to do...any ideas? Quote Link to comment Share on other sites More sharing options...
jpratt Posted June 9, 2009 Share Posted June 9, 2009 If the php setting will not work then you might have to change the .htaccess time limit for you script to run. I am not sure about your python script, you would probably have to go to a different board for that. Quote Link to comment Share on other sites More sharing options...
apw235 Posted June 9, 2009 Author Share Posted June 9, 2009 Just realized what the problem is: PHP was executing a Python script via a shell command, and then Python was trying to execute some shell commands - which I assumed would fork off of the process created by PHP, but it doesn't. The problem was that I wasn't thinking clearly, I can make the PHP script do all the hard work and Python just do the important stuff. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.