I want to be able to start a process and then be able to kill it afterwards
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
http://docs.python.org/library/os.html#process-management
Here's a little python script that starts a process, checks if it is running, waits a while, kills it, waits for it to terminate, then checks again. It uses the 'kill' command. Version 2.6 of python subprocess has a kill function. This was written on 2.5.
The timed output shows that it was terminated after about 3 seconds, and not 60 as the call to sleep suggests.
Have a look at the
subprocess
module. You can also use low-level primitives likefork()
via theos
module.A simple function that uses subprocess module:
see docs for primitive fork() and modules subprocess, multiprocessing, multithreading
If you need to interact with the sub process at all, I recommend the pexpect module (link text). You can send input to the process, receive (or "expect") output in return, and you can close the process (with force=True to send SIGKILL).