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 database
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?
I highly sugges using the multiprocessing library of Python. It has
process.terminate()methods.