是否可以修改以下代码以从'stdout'和'stderr'打印输出:
代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
def run_cmd(command, cwd=None):
p = subprocess.Popen(command, cwd=cwd, shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
outs, errs = p.communicate()
rc = p.returncode
outs = outs.decode('utf-8')
errs = errs.decode('utf-8')
return (rc, (outs, errs))
Run Code Online (Sandbox Code Playgroud)
感谢@unutbu,特别感谢@ jf-sebastian,最终功能:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from queue import Queue
from subprocess import PIPE, Popen
from threading import Thread
def read_output(pipe, funcs):
for line in iter(pipe.readline, b''):
for func in funcs: …Run Code Online (Sandbox Code Playgroud) 我有一个gevent应用程序,可以跨多个模块生成多个greenlet.我希望能够优雅地关闭应用程序(例如内部或通过捕获SIGTERM),允许greenlet通过捕获GreenletExit和执行finally:子句很好地终止.
如果我有一个所有正在运行的greenlets,我可以做gevent.killall(list_of_greenlets),但保持这样的列表是相当麻烦的; 此外,gevent必须以某种形式保留这个列表.
那么,我可以杀死已经启动的所有greenlets而不维护它们的列表吗?
(我在raspthon上的python 2.7上使用gevent 1.0.0)