eal*_*eon 4 python multithreading python-2.7
假设我有类似的东西:
def worker(name):
time.sleep(10)
print name
return
thrs = []
for i in range(1000):
t1 = threading.Thread(target=worker, args=(i,))
thrs.append(t1)
for t in thrs:
t.start()
Run Code Online (Sandbox Code Playgroud)
有没有办法指定并行运行多少个线程?在上面的例子中,所有1000将并行运行
这可以使用multiprocessing.dummy它来提供multiprocessingapi 的线程版本.
from multiprocessing.dummy import Pool
pool = Pool(10)
result = pool.map(worker, range(1000))
Run Code Online (Sandbox Code Playgroud)
在python 3中,concurrent.futures.ThreadPoolExecutor通常提供更好的界面.
| 归档时间: |
|
| 查看次数: |
3206 次 |
| 最近记录: |