简体   繁体   English

无法在子进程中获取二进制stdin / stdout

[英]Unable to get binary stdin/stdout in Subprocesses

I've been trying to implement a subprocessing application using the Parallel Python ( pp ) module, which is using subprocess with subprocess.PIPE to pass serialized instructions and responses between master and worker processes. 我一直在尝试使用Parallel Python( pp )模块实现子处理应用程序,该模块将subprocess进程与subprocess.PIPE一起使用,以在主进程和工作进程之间传递序列化的指令和响应。 I've had intermittent issues with the workers' stdin.read() commands returning empty strings rather than the normal behavior of blocking until a command is received. 工作人员的stdin.read()命令返回空字符串时遇到间歇性问题,而不是正常的阻塞行为,直到收到命令为止。

After a bit of research, I believe the cause is the worker processes' io streams are in text mode and attempting to pass pickled objects. 经过一些研究,我相信原因是工作进程的io流处于文本模式,并试图传递腌制的对象。 They eventually get something that looks like an EOF and then automatically return empty. 他们最终得到的东西看起来像EOF,然后自动返回空。 Looking in the pp source code, it even specifies the -u flag in its calling sequence, but the resulting workers' streams don't seem to be in binary mode, even though the calling Python interpreter is. pp源代码中查看时,它甚至在其调用序列中指定-u标志,但即使调用的Python解释器在其中,生成的工作程序流也似乎不在二进制模式下。 The fix suggested here and elsewhere is to use the msvcrt module to change the io format to binary, but for some reason it doesn't seem to have any effect. 此处和其他地方建议的修复方法是使用msvcrt模块将io格式更改为二进制,但是由于某种原因,它似乎没有任何作用。

I made the following demo script. 我制作了以下演示脚本。 This is Python 2.7.12, 32-bit, in Windows 10: 这是Windows 10中的Python 2.7.12(32位):

#master.py
import subprocess

if __name__ == '__main__':

    foo = subprocess.Popen(
            args = ['pythonw.exe','-u','-m','worker'],
            stdin = subprocess.PIPE,
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE,
        )

    open('master_log.txt','a').write(str(foo.stdin) + '\n')
    open('master_log.txt','a').write(str(foo.stdout) + '\n')

...and in the same folder... ...并在同一文件夹中...

#worker.py
import sys, os, msvcrt

if __name__ == '__main__':
    open('worker_log.txt','a').write('Initial Properties\n')
    open('worker_log.txt','a').write(str(sys.argv[0]) + '\n')
    open('worker_log.txt','a').write(str(sys.stdin) + '\n')
    open('worker_log.txt','a').write(str(sys.stdout) + '\n')
    open('worker_log.txt','a').write('Applying msvcrt.setmode()\n')
    msvcrt.setmode(sys.stdin.fileno(),os.O_BINARY)
    msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY)
    open('worker_log.txt','a').write(str(sys.stdin) + '\n')
    open('worker_log.txt','a').write(str(sys.stdout) + '\n')

From a windows command prompt: 在Windows命令提示符下:

python -u -m master.py

yields: 收益率:

#master_log.txt
<open file '<fdopen>', mode 'wb' at 0x02FA06A8>
<open file '<fdopen>', mode 'rb' at 0x02FA0D30>

#worker_log.txt
Initial Properties
C:\Users\204040537\Documents\Python\pygtp_addin\worker.py
<open file '<stdin>', mode 'r' at 0x031BD020>
<open file '<stdout>', mode 'w' at 0x031BD078>
Applying msvcrt.setmode()
<open file '<stdin>', mode 'r' at 0x031BD020>
<open file '<stdout>', mode 'w' at 0x031BD078>

Suggestions? 建议? I'm at a loss for how else to coerce the subprocess IO streams to read binary. 我不知道如何强制子进程IO流读取二进制文件。

Whelp, the resolution to this problem (appears) to be that a something related to an internal DLL my workers are using is causing my python interpreter to behave erratically. Whelp,此问题的解决方案(似乎)是与工作人员正在使用的内部DLL相关的某件事导致我的python解释器行为异常。 Interactive Python sessions exit after the line containing the offending command is complete, even if there are multiple statements on the line, eg 交互式Python会话在包含有问题的命令的行完成后退出,即使该行上有多个语句,例如

result = offending_command(some_data); print result

will run, and print the correct result, but the interpreter will immediately exit. 将运行,并输出正确的结果,但是解释器将立即退出。 Using the offending command in a python script and calling via execfile() will run normally, but the python interpreter immediately exits upon completion. 在python脚本中使用有问题的命令并通过execfile()调用将正常运行,但是python解释器在完成后立即退出。 Variations on some_data can repeatably cause or prevent this behavior. some_data上的some_data可重复导致或阻止此行为。

My workers continue to run, as evidenced by ongoing error log entries, but their stdin pipe seems broken? 我的工人继续运行,这由不断出现的错误日志条目证明,但是他们的stdin管道似乎坏了? Log messages indicate the stdin pipe is still open. 日志消息指示stdin管道仍处于打开状态。 In any case, this does not appear to be an issue with subprocess. 无论如何,这似乎不是子流程的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM