python stdin.write_向stdin写入大量数据

您可能必须使用Popen.communicate()。在

如果您向stdin写入大量数据,并且在此期间子进程生成输出到stdout,那么在处理所有stdin数据之前,子进程的stdout缓冲区可能会变满。子进程在写入stdout时阻塞(因为您没有读取它),而您在写入stdin时被阻止。在

Popen.communicate()可用于同时写入stdin和读取stdout/stderr,以避免前面的问题。在

注意:Popen.communicate()只有在输入和输出数据能够适应您的内存时才适用(它们不是太大)。在

更新:

如果您决定使用线程来解决问题,这里有一个父进程和子进程实现示例,您可以根据您的需要进行定制:

父级.py:#!/usr/bin/env python2

import os

import sys

import subprocess

import threading

import Queue

class MyStreamingSubprocess(object):

def __init__(self, *argv):

self.process = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

self.stdin_queue = Queue.Queue()

self.stdout_queue = Queue.Queue()

self.stdin_thread = threading.Thread(target=self._stdin_writer_thread)

self.stdout_thread = threading.Thread(target=self._stdout_reader_thread)

self.stdin_thread.start()

self.stdout_thread.start()

def process_item(self, item):

self.stdin_queue.put(item)

return self.stdout_queue.get()

def terminate(self):

self.stdin_queue.put(None)

self.process.terminate()

self.stdin_thread.join()

self.stdout_thread.join()

return self.process.wait()

def _stdin_writer_thread(self):

while 1:

item = self.stdin_queue.get()

if item is None:

# signaling the child process that the end of the

# input has been reached: some console progs handle

# the case when reading from stdin returns empty string

self.process.stdin.close()

break

try:

self.process.stdin.write(item)

except IOError:

# making sure that the current self.process_item()

# call doesn't deadlock

self.stdout_queue.put(None)

break

def _stdout_reader_thread(self):

while 1:

try:

output = self.process.stdout.readline()

except IOError:

output = None

self.stdout_queue.put(output)

# output is empty string if the process has

# finished or None if an IOError occurred

if not output:

break

if __name__ == '__main__':

child_script_path = os.path.join(os.path.dirname(__file__), 'child.py')

process = MyStreamingSubprocess(sys.executable, '-u', child_script_path)

try:

while 1:

item = raw_input('Enter an item to process (leave empty and press ENTER to exit): ')

if not item:

break

result = process.process_item(item + '\n')

if result:

print('Result: ' + result)

else:

print('Error processing item! Exiting.')

break

finally:

print('Terminating child process...')

process.terminate()

print('Finished.')

儿童.py:

^{pr2}$

注意:IOError在读/写线程上处理,以处理子进程退出/崩溃/终止的情况。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值