popen被阻塞

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?

阅读 4.7k
1 个回答

popen用法错了,它的第一个参数是字符串或者字符串数组,
如popen('cat /tmp/log')或者popen(['cat', '/tmp/log'])。

你的代码里在popen中直接调用hello,直接死循环,并没有开启新的进程。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题