开发者

Kill a Python process if it doesn't finish in a certain time?

Here's the scenario: I have a Python script that is called from a browser with AJAX. I want the Python script to run and return its output, however if it fails to successfully run in 10 seconds, I want the script to abort and return some other process. Also, at the very beginning, I want to log what the request was.

Here's how I'm thinking of architecting it: Commander script Calls three scripts, spawned as subprocesses? 1. the main code to execute and return its result 2. a second script, that waits 10 seconds, then returns False 3. a third script, to log the request in a d开发者_如何学Pythonatabase The commander script will return either the result from (1) or an error if it hears back from (2).

How would you actually implement this? I can't figure out if I should use the threading library or the os library with subprocesses.

Or is there a better way to do this?


If this script is run under Unix-like system, you may use SIGALRM to do it. There's an example in python docs.


I highly sugges using the multiprocessing library of Python. It has process.terminate() methods.


check out: subprocess.Popen.poll and subprocess.Popen.terminate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜