# -*- coding: utf-8 -*-
import shlex
import os
import signal
import time
import threading
from subprocess import Popen, PIPE
def run_command(command):
process = Popen(shlex.split(command), stdout=PIPE)
st = time.time()
while True:
output = process.stdout.readline().rstrip().decode('utf-8')
if output == '' and process.poll() is not None:
break
if output:
print(output.strip())
if time.time() - st > 3:
os.kill(process.pid, signal.CTRL_C_EVENT)
break
rc = process.poll()
return rc
if __name__ == "__main__":
t = threading.Thread(target=run_command, args=("ping baidu.com",))
t.start()
标签:__,Python,Popen,subprocess,process,time,import
From: https://www.cnblogs.com/cokefentas/p/17629470.html