首頁  >  問答  >  主體

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?

高洛峰高洛峰2907 天前980

全部回覆(1)我來回復

  • 三叔

    三叔2016-11-04 10:07:52

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

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


    回覆
    0
  • 取消回覆