无法在Windows上收集子进程输出

时间:2018-04-08 20:05:30

标签: python subprocess

我正在尝试在Windows上收集子进程输出,但无法这样做。你能指出我做错了什么吗?

总的来说,grepword-p.py读取了一个搜索词'和文件/目录的参数列表'并产生'计数'从事这项工作的童工人数。我正在寻找收集每个工人的输出并对输出进行排序,这是我尝试的时候" Popening"使用stdout和stderr参数。但是我在下面显示错误 -

grepword-p.py

import os
import sys
import subprocess
import argparse

def parse_options():
    ....
    return args, args.word, args.files

def get_files(args, recurse):
    ...
    return files

child = os.path.join(os.path.dirname(__file__), "grepword-p-child.py")
opts, word, args = parse_options()
filelist = get_files(args, opts.recurse)
files_per_process = len(filelist) // opts.count
start, end = 0, files_per_process + (len(filelist) % opts.count)
number = 1

pipes=[]
while start < len(filelist):
    command = [sys.executable, child]
    if opts.debug:
        command.append(str(number))
    pipe = subprocess.Popen(
            command,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            # stderr=subprocess.PIPE,
        )
    pipe.stdin.write(word.encode("utf-8") + b"\n")
    for filename in filelist[start:end]:
        pipe.stdin.write(filename.encode("utf-8") + b"\n")
    pipe.stdin.close
    print("sent data to worker {}".format(number))
    number += 1
    start, end = end, end + files_per_process

try:
    stdouts, stderrs = ([], [])
    while pipes:
        pipe = pipes.pop()
        # pipe.wait
        out, err = pipe.communicate()
        stdouts.append(out.decode("utf-8", "ignore"))
        stderrs.append(err.decode("utf-8", "ignore"))
    print("Output from all workers\n{}\n".format(sorted(stdouts)))
    print("Error from all workers\n{}\n".format(sorted(stderrs)))

except Exception as err:
    exc_typ, exc_obj, exc_tb = sys.exc_info()
    print("Exception at line {}".format(exc_tb.lineno))

print("done")

grepword-p-child.py

import sys

BLOCK_SIZE = 1000

try:
    number = "{0}: ".format(sys.argv[1]) if len(sys.argv) == 2 else ""
    stdin = sys.stdin.buffer.read()
    lines = stdin.decode("utf-8", "ignore").splitlines()
    word = lines[0].rstrip()

    for filename in lines[1:]:
        filename = filename.rstrip()
        previous = ""
        try:
            with open(filename, "rb") as f:
                while True:
                    current = f.read(BLOCK_SIZE)
                    if not current:
                        break
                    current = current.decode("utf-8", "ignore")
                    if (word in current or
                        word in previous[-len(word):] + current[:len(word)]):
                        print("{0}{1}".format(number, filename))
                        break
                    if len(current) != BLOCK_SIZE:
                        break
                    previous = current
        except Exception as err:
            print("error here")
            print("{0}{1}".format(number, err))

except Exception as err:
    exc_typ, exc_obj, exc_tb = sys.exc_info()
    print("Exception at line {}".format(exc_tb.lineno))

输出

c:\Python27\user_modules>python grepword-p.py --count 2 --debug True --recurse False child c:\Python27\user_modules
sent data to worker 1
sent data to worker 2
Output from all workers
[]

Error from all workers
[]

done

c:\Python27\user_modules>Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp1252'>
OSError: [Errno 22] Invalid argument
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp1252'>
OSError: [Errno 22] Invalid argument

0 个答案:

没有答案