【发布时间】:2016-02-10 23:01:16
【问题描述】:
我正在尝试制作一个脚本,该脚本一次只使用 4 个进程,并在返回值后启动另一个。我认为有些问题是results.get
一直等待直到它得到结果,并且在返回值之前不会继续。我希望 While 循环在我等待结果时继续。
https://docs.python.org/2.7/library/multiprocessing.html#multiprocessing.pool.AsyncResult.get
import multiprocessing as mp
import time
from random import randint
def print_hello(VPN_Name):
time.sleep(randint(0,5))
return VPN_Name
VPN_list = ['3048-VPN01', '3049-VPN01', '3051-VPN01', '3053-VPN01', '3058-VPN01', '3059-VPN01', '3061-MULTI01', '3063-VPN01', '3065-VPN01', '3066-MULTI01', '3067-VPN01', '3069-VPN01', '3071-VPN01', '3072-VPN01']
VPN_len = len(VPN_list)
x = 0
pool = mp.Pool(processes=4)
job_tracker = []
complete_tracker = []
while True:
for VPN_Name in VPN_list:
if VPN_len == 0:
break
while True:
print "Complete Job Tracker ", complete_tracker
print "Job Tracker ", job_tracker
for comp in complete_tracker:
if comp in job_tracker:
x = x - 1
job_tracker.remove(comp)
print "Confirmed complete " + comp
continue
if x < 4:
results = pool.apply_async(print_hello,args=(VPN_Name,))
VPN_len = VPN_len - 1
x = x + 1
print "Started " + VPN_Name
job_tracker.append(VPN_Name)
complete_tracker.append(results.get())
break
continue
【问题讨论】:
标签: python python-2.7 asynchronous multiprocessing