在命令行中,我们可以使用管道符号连接多个命令,以便将一个命令的输出作为另一个命令的输入。例如,下面的命令行将“foo”输入firstCommand命令,并将firstCommand的输出作为secondCommand的输入,再将secondCommand的输出作为thirdCommand的输入,最后将thirdCommand的输出重定向到文件finalOutput。
$ echo foo | firstCommand - | secondCommand - | thirdCommand - > finalOutput
我们希望在Python脚本中重现这个命令行,以便在将firstCommand的输出传递给secondCommand之前对其进行处理,并在secondCommand和thirdCommand之间再次进行处理。
2、解决方案
为了解决这个问题,我们可以使用线程来创建多个子进程实例,并使用管道将这些子进程连接起来。下面是Python脚本的示例:
import subprocess
import threading
import sys
first_process = subprocess.Popen(['firstCommand', '-'], stdin=subprocess.PIPE,
http://www.jshk.com.cn/mb/reg.asp?kefu=xiaoding;//爬虫IP免费获取;stdout=subprocess.PIPE)
second_process = subprocess.Popen(['secondCommand', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
third_process = subprocess.Popen(['thirdCommand', '-'], stdin=subprocess.PIPE, stdout=sys.stdout)
first_thread = threading.Thread(target=consumeOutputFromStdin, args=(sys.stdin, first_process.stdin))
second_thread = threading.Thread(target=consumeOutputFromFirstCommand, args=(first_process.stdout, second_process.stdin))
third_thread = threading.Thread(target=consumeOutputFromSecondCommand, args=(second_process.stdout, third_process.stdin))
first_thread.start()
second_thread.start()
third_thread.start()
first_thread.join()
second_thread.join()
third_thread.join()
third_process.communicate()
# read 1K chunks from standard input
def consumeOutputFromStdin(from_stream, to_stream):
chunk = from_stream.read(1024)
while chunk:
to_stream.write(chunk)
to_stream.flush()
chunk = from_stream.read(1024)
def consumeOutputFromFirstCommand(from_stream, to_stream):
while True:
unprocessed_line = from_stream.readline()
if not unprocessed_line:
break
processed_line = some_python_function_that_processes_line(unprocessed_line)
to_stream.write(processed_line)
to_stream.flush()
def consumeOutputFromSecondCommand(from_stream, to_stream):
while True:
unprocessed_line = from_stream.readline()
if not unprocessed_line:
break
processed_line = a_different_python_function_that_processes_line(unprocessed_line)
to_stream.write(processed_line)
to_stream.flush()
在这个脚本中,我们首先创建了三个子进程实例,分别对应firstCommand、secondCommand和thirdCommand。然后,我们创建了三个线程,分别用于从标准输入读取数据并将其传递给firstCommand、从firstCommand的输出中读取数据并将其传递给secondCommand、从secondCommand的输出中读取数据并将其传递给thirdCommand。
最后,我们调用join()方法等待所有线程完成,然后调用communicate()方法从thirdCommand中读取输出。
这样,我们就成功地将三个命令连接在一起,并可以在Python脚本中对它们进行处理。