我正在使用龙卷风。我希望以下内容易于理解,它产生一个 tcproute 进程并将输出发送到 websocket 的另一端。
class TracerouteHandler(tornado.websocket.WebSocketHandler):
def open(self,ip):
p = subprocess.Popen(['traceroute',ip],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while p.poll() is None: #running
line=p.stdout.readline()
if line!=None and len(line)>0:
self.write_message(json.dumps({'stdout':line}))
self.write_message(json.dumps({'stdout':'Done! For red marked ip addresses, the location couldn\'t be determined with <a href="http://hostip.info">hostip.info</a>. If you know the correct location, please update it there.'}))
问题是由于while p.poll() is None
循环,龙卷风块。此外,如果 p.stdout 中没有可读取的内容,则p.stdout.readline
块。所以我想要某种回调机制,当我在p.stdout
. 我怎么做?