How to launch a background process in Python with a delayed invocation? -
OK, this is a bit of a prickly problem. I need to start a background process that will wait for (1) n seconds, and then (2) execute some commands. In addition, I need to capture the PID of the background process, because when the basic process is finished, the background process will be killed when necessary. It looks a bit in Shell syntax:
(sleep 15 and run_som_com) & amp; # Launch BG process bg_pid = $! Capture #bg pid ... here's some stuff ... kill -9 $ bg_pid # and kill bg_pid if necessary
then in shell Launch is. However, I'm trying to do it in Python. The hard thing is that since I have to capture BG Pod, I can not use the OSSystem () or OSFork (), because in every case the parent does not have access to the PID in the process. I am trying to work with Subprakashi. Popen (), but the "sleeping 15" part is a bit tricky.
Any thoughts? I can not use os.system () or os.fork (),
Because in every case the basic process is not accessible, the child's affair
er, the parent gets the PID as a return value from fork ()
is. In any case, I would suggest using the module. This gives you access to pid through the pid
attribute of the popen object. You can use the function to send a signal in a process (to kill it).
Basically, I've included the effect of "subprosos in a comment because it works on the window and fork (t)". It was removed almost immediately, because the sub-publication module works, osc. () does not. However, you can also work with this by defining a soft function for Windows, as well as the suggestion is made and by Alex Martelli himself (some ironic), I can now really declare that Subprosy has the advantage of being a more cross platform, and this is something good.
Of course, if you use the answer of Alex Martley for this question, then the ability to run is reduced to Windows again, because you depend on the features of the system shell (Siegwin is always a Option).
Comments
Post a Comment