def hello(): while True: print 'subprocess = ' + str(os.getpid()) # time.sleep(1) def killPid(pid): print 'kill ' + str(pid) os.system("taskkill" + ' /T /F /pid '+ str(pid)) p = subprocess.Popen(hello()) # p = subprocess.Popen("ping 10.193.101.34", shell=True) print 'after subprocess' t = threading.Timer(3.0, killPid, args=(p.pid,)) t.start() # after 3 seconds, "hello, world" will be printed
为啥subprogress没被kill?
popen用法错了,它的第一个参数是字符串或者字符串数组,如popen('cat /tmp/log')或者popen(['cat', '/tmp/log'])。
你的代码里在popen中直接调用hello,直接死循环,并没有开启新的进程。