【发布时间】:2016-02-08 12:23:34
【问题描述】:
我想创建一个命令,在给定数量的文件中并行搜索给定单词,其中...
ppatternsearch [-p n] word {files}
-
ppatternsearch是命令名 -
-p是一个定义并行化级别的选项 n是 -p 选项将执行的进程/线程数 为单词搜索创建word是我要搜索的词-
files是我将要搜索的文件。
我想通过两种方式做到这一点 - 一种使用processes,另一种使用threads。最后,父进程/主线程返回它找到正在搜索的单词的行数。
问题是,我已经开发了一些代码,但我碰壁了。我不知道从这里去哪里。
import argparse, os, sys, time
num_lines_with_pattern = []
def pattern_finder(pattern, file_searched):
counter = 0
with open(file_searched, 'r') as ficheiro_being_read:
for line in ficheiro_being_read:
if pattern in line:
print line
counter += 1
num_lines_with_pattern.append(counter)
parser = argparse.ArgumentParser()
parser.add_argument('-p', type = int, default = 1, help = Defines command parallelization.')
args = parser.parse_args()
【问题讨论】:
标签: python multithreading python-2.7 python-3.x subprocess