我的子流程代码有问题.该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**
Run Code Online (Sandbox Code Playgroud)
当我更改hcitool lescan为ping www.google.com时,此代码工作正常,并产生输出但是当我尝试使用hcitool lescan它时,要么永久挂起,要么不产生输出.感谢帮助!
小智 5
以上任何一个答案对我都不起作用.挂在hcitool的永远扫描.最后我写了一个shell脚本并用我的python代码调用它.这对我来说很好,我正在读取文件"result.txt"的输出.
hcitool lescan>result.txt &
sleep 5
pkill --signal SIGINT hcitool
Run Code Online (Sandbox Code Playgroud)