-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmultiple.py
More file actions
42 lines (38 loc) · 1008 Bytes
/
Copy pathmultiple.py
File metadata and controls
42 lines (38 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding : utf-8 -*-
# @Author : Coco
# @Author's GitHub : https://github.com/COCO5666
# @Author's CSND : https://blog.csdn.net/COCO56
# @Author's Webpage : https://coco5666.github.io/
# @IDE: PyCharm
# @Python: Python3.7.2
# @Created Time :2019-02-16 19:50:00
# @Modified Time :2019-02-16 23:58:25
# @File : multiple.py
"""
加速模块
暂时提供多进程加速(注意在使用多进程进行异步运行时不能运行采用单例模式设计的类的方法)
"""
import multiprocessing
import os
def asyncRunFunc(func, paraList, poolNum=None, asyn=True):
if poolNum == None:
poolNum = os.cpu_count() // 2
if poolNum < 1:
poolNum = 1
results = []
if asyn:
pool = multiprocessing.Pool(poolNum)
for para in paraList:
# print(para)
results.append(pool.apply_async(func, (para,)))
pool.close()
pool.join()
data = []
for res in results:
data.append(res.get())
else:
for para in paraList:
# print(para)
results.append(func(para))
data = results
return data