我正在为我自己的目的调整这个Django管理命令.该脚本是一个简单的while循环守护进程,它command.handle()根据协议从sys.stdin(第152行)读取并将结果写入sys.stdout.
我希望sys.stdin.read()阻止它收到的东西,但我发现当我运行这个脚本时,它会在发送或接收任何数据之前吃掉100%的CPU.
sys.stdin.read(n)阻止?time.sleep(s)使用安全,或者我会想念输入或者是反应慢了?我正在尝试使用 python 为 bash 会话编写一个包装器。我做的第一件事就是尝试生成一个 bash 进程,然后尝试读取它的输出。像这样:
from subprocess import Popen, PIPE
bash = Popen("bash", stdin = PIPE, stdout = PIPE, stderr = PIPE)
prompt = bash.stdout.read()
bash.stdin.write("ls\n")
ls_output = bash.stdout.read()
Run Code Online (Sandbox Code Playgroud)
但这不起作用。首先,在创建进程后从 bash 的标准输出读取失败,当我尝试写入标准输入时,出现管道损坏错误。我究竟做错了什么?
再次澄清一下,我对通过 bash 运行单个命令然后检索其输出不感兴趣,我希望在某个进程中运行一个 bash 会话,我可以通过管道与之进行通信。
文件sp.py:
#!/usr/bin/env python3
s = input('Waiting for your input:')
print('Data:' + s)
Run Code Online (Sandbox Code Playgroud)
文件main.py
import subprocess as sp
pobj = sp.Popen('sp.py',stdin=sp.PIPE,stdout=sp.PIPE,shell=True)
print(pobj.stdout.read().decode())
pobj.stdin.write(b'something...')
print(pobj.stdout.read().decode())
Run Code Online (Sandbox Code Playgroud)
main.py会阻塞第一个pobj.stdout.read(),因为sp.py在等我.
但是如果我想首先处理字符串'Waiting for you input:',我怎么知道sp.py是否在等我呢?
换句话说,我希望pobj.stdout.read()在sp.py等待(或因为time.sleep())而休眠时返回.
我在Python中实现了一个非阻塞读取器,我需要提高它的效率.
背景:我需要从一个子进程(从Popen()开始)读取大量输出并传递给另一个线程.读取该子进程的输出不得阻塞超过几毫秒(最好是读取可用字节所需的时间很短).
目前,我有一个实用程序类,它接受文件描述符(stdout)和超时.我select()和readline(1)直到三件事情之一碰巧:
然后我将缓冲的文本返回给调用方法,该方法用它来完成.
现在,对于真正的问题:因为我正在阅读如此多的输出,我需要提高效率.我想通过询问文件描述符有多少字节待处理来做到这一点readline([that many bytes]).它应该只是传递东西,所以我实际上并不关心新线,或者即使有任何.我可以问一下文件描述符可以读取多少字节,如果可以,怎么做?
我已经做了一些搜索,但是我很难找到要搜索的内容,更不用说是否可能了.
即使是正确方向的一点也会有所帮助.
注意:我正在开发Linux,但这对于"Pythonic"解决方案来说无关紧要.
这个问题不是重复的
这个问题得到了解决,因为它的用例允许输入一起发送,但如果您的程序是交互式的(如此处的用例所示),则情况并非如此。
文件subprocess.Popen说:
communicate(input=None)
Interact with process: Send data to stdin. Read data from stdout
and stderr, until end-of-file is reached. Wait for process to
terminate. ...
Run Code Online (Sandbox Code Playgroud)
是否可以在子进程终止之前与子进程进行多次通信,例如与终端或网络套接字?
例如,如果子进程是bc,则父进程可能希望根据需要向其发送不同的输入以进行计算。由于发送到的输入bc可能取决于用户输入,因此不可能一次发送所有输入。
在 python 中,如何检查 subprocess.Popen 对象的标准输出以获取任何要读取的内容?我正在围绕一个工具编写一个包装器,该工具有时会连续运行几个小时。当运行时间超过几分钟时,在子进程的标准输出上使用 .readline() 会严重降低脚本的速度。如果有任何内容需要阅读,我需要一种更有效地检查标准输出的方法。顺便说一句,这个特定的工具一次只能写入完整的行。脚本是这样的:
#!/usr/bin/python -u
#thiswrap.py
import sys, time
from subprocess import *
chldp = Popen(sys.argv[1], bufsize=0, stdout=PIPE, close_fds=True)
chstdin,chstdout=chldp.stdin,chldp.stdout
startnoti=False
while not chldp.poll():
rrl=chstdout.readline() # <--- this is where the problem is
if rrl[-8:]=='REDACTED TEXT':
sys.stdout.write(rrl[:-1]+' \r')
if not startnoti: startnoti=True
else:
if startnoti: sys.stdout.write('\n')
sys.stdout.write(rrl)
if startnoti: # REDACTED
time.sleep(0.1)
time.sleep(0.1)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有一个python程序,它使用Popen并在生成时几乎实时地使用它们的输出来启动子进程.相关循环的代码是:
def run(self, output_consumer):
self.prepare_to_run()
popen_args = self.get_popen_args()
logging.debug("Calling popen with arguments %s" % popen_args)
self.popen = subprocess.Popen(**popen_args)
while True:
outdata = self.popen.stdout.readline()
if not outdata and self.popen.returncode is not None:
# Terminate when we've read all the output and the returncode is set
break
output_consumer.process_output(outdata)
self.popen.poll() # updates returncode so we can exit the loop
output_consumer.finish(self.popen.returncode)
self.post_run()
def get_popen_args(self):
return {
'args': self.command,
'shell': False, # Just being explicit for security's sake
'bufsize': 0, # More likely …Run Code Online (Sandbox Code Playgroud) 我正在使用subprocess.Popen()的一个非常基本的设置,并将stdout指向一个变量,我稍后将其返回到我的python脚本的不同部分.
这是我的基本Popen代码:
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# wait for the process to terminate
out, err = process.communicate()
errcode = process.returncode
print out
Run Code Online (Sandbox Code Playgroud)
这适用于许多类似ls -al或类似的基本用例.但是,我想知道如何处理从更长(或无限期)运行过程中定期和一致地获取输出,例如tail -f foo.log.有没有办法在循环中定期读取stdout?或者产生一个线程来检查并定期返回每个线程?这里最好的方法是什么?
谢谢!
我正在努力使rxpy函数反应式编程(FRP)进入图书馆,而我已经遇到了障碍。我正在编写一个小程序,希望通过标准输入(sys.stdin)来流式传输数据。
因此,我的问题很简单:如何创建一个rx.Observable从stdin异步读取的实例?是否有内置机制可Observable从流创建实例?
我想知道是否有任何方法可以处理非阻塞的情况以及从 python 子进程流式传输stdout和stderr的情况。我已经通过这个网站浏览了几篇文章,但没有一篇文章能回答这种情况。它要么通过在另一个线程中运行读取来显示,以便主线程不受影响(或)只是如何从进程中流式传输数据。我的问题是两者的结合。我想问题出在read()或readline()因为它们在输出流期间以一种或另一种方式阻塞。
我想要一个可流式传输的对象,这是我的主要关注点。我附上我已经通过的参考资料以供进一步参考。
script_process = subprocess.Popen(command_sequence[i],preexec_fn=os.setsid, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while script_process.poll() is None:
for output in iter(lambda: script_process.stdout.readline(), b''):
sys.stdout.writelines(output)
Run Code Online (Sandbox Code Playgroud)
python ×10
subprocess ×6
popen ×4
stdout ×3
bash ×1
django ×1
ipc ×1
nonblocking ×1
python-2.7 ×1
python-3.x ×1
readline ×1
rx-py ×1
shell ×1
stdin ×1