2017-09-16 80 views
0

當我在Python中使用子進程重定向標準輸出時,吞吐量非常低。我做錯了嗎?使用python中的子進程重定向標準輸出速度非常慢

基本上,我管道外部程序的標準輸出放入隊列中。然後在另一個功能中,我將它打印在控制檯中。

這裏是hexdump都一個示例代碼來生成隨機輸出:

from subprocess import Popen, PIPE 
from queue import Queue 
import sys 
from threading import Thread, Event 
import threading 

class Buffer(Queue): 

    def __init__(self, *args, **kwargs): 
     Queue.__init__(self, *args, **kwargs) 

    def write(self, line): 
     self.put_nowait(line) 
     self.join() 

    def read(self): 
     element = self.get_nowait() 
     self.task_done() 
     return element 

def write_output(buffer, stopped): 

    hexdump = Popen(['hexdump', '-C', '/dev/urandom'], stdout=PIPE) 
    while hexdump.returncode is None: 
     for line in hexdump.stdout.readlines(8192): 
      buffer.write(line) 
      if stopped.is_set(): 
       hexdump.terminate() 
       hexdump.wait() 
       print('process terminated.') 
       break 

def read_output(buffer, stopped): 
    while not stopped.is_set(): 
     while not buffer.empty(): 
      output = buffer.read() 
      print('********* output: {}'.format(output)) 
      sys.stdout.flush() 
    print('stopped') 
    sys.stdout.flush() 


buffer = Buffer() 
stopped = Event() 


generate_random_output = Thread(target=write_output, args=(buffer, stopped)) 
generate_random_output.name = 'generate_random_output' 
generate_random_output.start() 

process_output = Thread(target=read_output, args=(buffer, stopped)) 
process_output.name = 'process_output' 
process_output.start() 

try: 
    while True: 
     continue 
except KeyboardInterrupt: 
    stopped.set() 
    generate_random_output.join() 
    process_output.join() 
    print('finished generating') 
    print('finished processing') 

我希望得到任何幫助。

回答

0

不是重定向你的輸出隊列 - 直接過程是:

def write_output(buffer, stopped): 

    hexdump = Popen(['hexdump', '-C', '/dev/urandom'], stdout=PIPE) 
    while hexdump.poll() is None: 
     while not stopped.is_set(): 
      for line in iter(hexdump.stdout.readline, b''): 
       print('********* output: %s' % line.decode(), end='') 
       sys.stdout.flush() 

     hexdump.terminate() 
     hexdump.wait() 
     print('process terminated.') 
     break 
+0

謝謝!有時stdout會掛起,所以我想添加8192來獲得輸出塊,但我會以這種方式再試一次。 –

相關問題