我在year_queue中有一个大约15年的列表,我需要每年生成一个进程。但是根据我运行代码的服务器,处理器的数量会有所不同。如何根据服务器中的处理器数量动态改变变量num_processes?
如果我设置了num_processes>处理器的数量,会自动产生相应的产生吗?当我测试它时 - 它创建了15个进程&在它们之间分配CPU电源。我正在寻找一种方法来首先创建'n'个进程,其中n =服务器中的处理器数量,然后当每个进程完成时,下一个进程生成。
for i in range(num_processes):
worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
print "worker spawned for " + str(i)
worker.start()
results = []
while len(results) < len(years):
result = result_queue.get()
results.append(result)
任何人都有同样的问题吗?
while year_queue.empty() != True:
for i in range(num_processes):
worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
print "worker spawned for " + str(i)
worker.start()
# collect results off the queue
print "results being collected"
results = []
while len(results) < len(num_processes):
result = result_queue.get()
results.append(result)
答案 0 :(得分:3)
使用multiprocessing Pool。该课程完成了为您选择正确数量的流程并运行它们的所有繁琐工作。它也不会为每个任务生成一个新进程,但是一旦完成就重用它们。
def process_year(year):
...
return result
pool = multiprocessing.Pool()
results = pool.map(process_year, year_queue)
答案 1 :(得分:0)
from multiprocessing import Process, Queue, cpu_count
from Queue import Empty
class ForEachPerson(Process):
def __init__(self, year_queue, result_queue, i, dict_of_files):
self.year_queue=year_queue
self.result_queue=result_queue
self.i=i
self.dict_of_files=dict_of_files
super(ForEachPerson, self).__init__()
def run(self):
while True:
try:
year=self.year_queue.get()
''' Do something '''
self.result_queue.put(year)
except Empty:
self.result_queue.close()
return
if __name__ == '__main__':
year_queue=Queue()
result_queue=Queue()
dict_of_files={}
start_year=1996
num_years=15
for year in range(start_year, start_year + num_years):
year_queue.put(year)
workers=[]
for i in range(cpu_count()):
worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
print 'worker spawned for', str(i)
worker.start()
workers.append(worker)
results=[]
while len(results) < num_years:
try:
year=result_queue.get()
results.append(year)
print 'Result:', year
except Empty:
pass
for worker in workers:
worker.terminate()