运行 来自 for 循环的多个程序
Running multiple programs from the for loop
这个程序一个一个打印出每个文本文件的行数:
files = glob.glob('*.txt') # 5 files
for f in files:
with open(f,'r') as fi:
lines = fi.read().splitlines()
print len(lines)
如何编写我的代码,使其同时运行 5 个程序并分别打印每个程序的行数?
这是您的程序的多线程版本:
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
这个程序一个一个打印出每个文本文件的行数:
files = glob.glob('*.txt') # 5 files
for f in files:
with open(f,'r') as fi:
lines = fi.read().splitlines()
print len(lines)
如何编写我的代码,使其同时运行 5 个程序并分别打印每个程序的行数?
这是您的程序的多线程版本:
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