从for循环运行多个程序

时间:2015-02-24 18:03:33

标签: python multithreading performance multiprocessing

该程序逐个打印出每个文本文件的行数:

files = glob.glob('*.txt') # 5 files
for f in files:
  with open(f,'r') as fi:
     lines = fi.read().splitlines()
     print len(lines)

如何编写我的代码,使其同时运行5个程序并分别打印每个程序的行数?

1 个答案:

答案 0 :(得分:2)

以下是程序的多线程版本:

import threading

# called by each thread
def do_task(f) :
  with open(f,'r') as fi:
     lines = fi.read().splitlines()
     print len(lines)

threads = []
files = glob.glob('*.txt') # 5 files
for f in files:
    t = threading.Thread(target=do_task, args = (f))
    threads.append(t)
    t.start()

for t in threads :
    t.join()

这里有趣的是多进程版本:

from multiprocessing import Process

# called by each thread
def do_task(f, l) :
  with open(f,'r') as fi:
     lines = fi.read().splitlines()
     l.append(lines)

lengths = []
processes = []
files = glob.glob('*.txt') # 5 files
for f in files:
    p = Process(target=do_task, args = (f,lengths,))
    processes.append(p)
    p.start()

for p in processes :
    p.join()

for l in lengths:
    print l
相关问题