我的子流程代码有问题。工作正常,subprocess.Popen()
但是当我尝试通过它读取它的输出时stdout.read()
,没有值可以读取。
**import os
import signal
import subprocess
import threading
import sys
import commands
print commands.getoutput("hcitool dev")
print 'down'
commands.getoutput('hciconfig hci1 down')
print 'up'
commands.getoutput('hciconfig hci1 up')
commands.getoutput('killall hcitool')
stop = False
ping = subprocess.call('hcitool lescan', shell = False,
stdout=subprocess.PIPE,executable='/bin/bash')
for i in ping.stdout:
print i
def kill():
global stop
stop = True
os.kill(ping.pid, signal.SIGTERM)
threading.Timer(5, kill).start()
#while not stop:
# print 'now in while not loop'
# sys.stdout.write(ping.stdout.read(1))
print 'trying to print stdout'
out, err = ping.communicate()
print "out",out
#result = out.decode()
print "Result : ",result**
当我更改为 ping www.google.com 并产生输出时,此代码工作正常hcitool lescan
,但是当我尝试使用它时,hcitool lescan
它要么永远挂起,要么不产生输出。帮助表示赞赏!