这可能是一个微不足道的问题,但我如何在python中并行化以下循环?
# setup output lists
output1 = list()
output2 = list()
output3 = list()
for j in range(0, 10):
# calc individual parameter value
parameter = j * offset
# call the calculation
out1, out2, out3 = calc_stuff(parameter = parameter)
# put results into correct output list
output1.append(out1)
output2.append(out2)
output3.append(out3)
我知道如何在Python中启动单线程,但我不知道如何“收集”结果。
多个过程也可以 - 对于这种情况最简单的事情。我正在使用当前的Linux,但代码也应该在Windows和Mac上运行。
并行化此代码的最简单方法是什么?
答案 0 :(得分:135)
由于全局解释器锁定(GIL),在CPython上使用多个线程不会为纯Python代码提供更好的性能。我建议改为使用multiprocessing
模块:
pool = multiprocessing.Pool(4)
out1, out2, out3 = zip(*pool.map(calc_stuff, range(0, 10 * offset, offset)))
请注意,这在交互式解释器中不起作用。
为了避免围绕GIL进行通常的FUD:无论如何使用线程都没有任何优势。你希望在这里使用进程,而不是线程,因为它们避免了一大堆问题。
答案 1 :(得分:45)
为了并行化一个简单的for循环,joblib为多处理的原始使用带来了很多价值。不仅是短语法,而且还有迭代的透明聚合,当它们非常快(消除开销)或捕获子进程的回溯时,可以有更好的错误报告。
免责声明:我是joblib的原作者。
答案 2 :(得分:36)
并行化此代码的最简单方法是什么?
我非常喜欢concurrent.futures
,可以在Python3 since version 3.2中找到 - 并通过backport到PyPi上的2.6和2.7。
您可以使用线程或进程并使用完全相同的界面。
将它放在一个文件中 - futuretest.py:
import concurrent.futures
import time, random # add some random sleep time
offset = 2 # you don't supply these so
def calc_stuff(parameter=None): # these are examples.
sleep_time = random.choice([0, 1, 2, 3, 4, 5])
time.sleep(sleep_time)
return parameter / 2, sleep_time, parameter * parameter
def procedure(j): # just factoring out the
parameter = j * offset # procedure
# call the calculation
return calc_stuff(parameter=parameter)
def main():
output1 = list()
output2 = list()
output3 = list()
start = time.time() # let's see how long this takes
# we can swap out ProcessPoolExecutor for ThreadPoolExecutor
with concurrent.futures.ProcessPoolExecutor() as executor:
for out1, out2, out3 in executor.map(procedure, range(0, 10)):
# put results into correct output list
output1.append(out1)
output2.append(out2)
output3.append(out3)
finish = time.time()
# these kinds of format strings are only available on Python 3.6:
# time to upgrade!
print(f'original inputs: {repr(output1)}')
print(f'total time to execute {sum(output2)} = sum({repr(output2)})')
print(f'time saved by parallelizing: {sum(output2) - (finish-start)}')
print(f'returned in order given: {repr(output3)}')
if __name__ == '__main__':
main()
这是输出:
$ python3 -m futuretest
original inputs: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
total time to execute 33 = sum([0, 3, 3, 4, 3, 5, 1, 5, 5, 4])
time saved by parallellizing: 27.68999981880188
returned in order given: [0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
现在将ProcessPoolExecutor
更改为ThreadPoolExecutor
,然后再次运行该模块:
$ python3 -m futuretest
original inputs: [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
total time to execute 19 = sum([0, 2, 3, 5, 2, 0, 0, 3, 3, 1])
time saved by parallellizing: 13.992000102996826
returned in order given: [0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
现在你已经完成了多线程和多处理!
抽样太小,无法比较结果。
但是,我怀疑多线程通常比多处理更快,特别是在Windows上,因为Windows不支持分配,所以每个新进程都需要花时间启动。在Linux或Mac上,他们可能会更接近。
您可以在多个进程中嵌套多个线程,但建议不要使用多个线程来分离多个进程。
答案 3 :(得分:15)
from joblib import Parallel, delayed
import multiprocessing
inputs = range(10)
def processInput(i):
return i * i
num_cores = multiprocessing.cpu_count()
results = Parallel(n_jobs=num_cores)(delayed(processInput)(i) for i in inputs)
print(results)
以上在我的机器上运行得很漂亮(Ubuntu,包joblib已预先安装,但可以通过pip install joblib
安装)。
答案 4 :(得分:6)
使用Ray有很多优点:
以您为例,您可以启动Ray并定义一个远程功能
import ray
ray.init()
@ray.remote(num_return_vals=3)
def calc_stuff(parameter=None):
# Do something.
return 1, 2, 3
然后并行调用
output1, output2, output3 = [], [], []
# Launch the tasks.
for j in range(10):
id1, id2, id3 = calc_stuff.remote(parameter=j)
output1.append(id1)
output2.append(id2)
output3.append(id3)
# Block until the results have finished and get the results.
output1 = ray.get(output1)
output2 = ray.get(output2)
output3 = ray.get(output3)
要在集群上运行相同的示例,唯一会改变的行是对ray.init()的调用。相关文档可以在here中找到。
请注意,我正在帮助开发Ray。
答案 5 :(得分:3)
为什么不使用线程和一个互斥锁来保护一个全局列表?
import os
import re
import time
import sys
import thread
from threading import Thread
class thread_it(Thread):
def __init__ (self,param):
Thread.__init__(self)
self.param = param
def run(self):
mutex.acquire()
output.append(calc_stuff(self.param))
mutex.release()
threads = []
output = []
mutex = thread.allocate_lock()
for j in range(0, 10):
current = thread_it(j * offset)
threads.append(current)
current.start()
for t in threads:
t.join()
#here you have output list filled with data
请记住,您将与最慢的线程一样快
答案 6 :(得分:2)
我发现joblib
对我很有用。请参见以下示例:
from joblib import Parallel, delayed
def yourfunction(k):
s=3.14*k*k
print "Area of a circle with a radius ", k, " is:", s
element_run = Parallel(n_jobs=-1)(delayed(yourfunction)(k) for k in range(1,10))
n_jobs = -1:使用所有可用的内核
答案 7 :(得分:1)
在Python中实现多处理和并行/分布式计算时,这可能很有用。
YouTube tutorial on using techila package
Techila是一个分布式计算中间件,它使用techila软件包直接与Python集成。包中的桃函数可用于并行化循环结构。 (以下代码段来自Techila Community Forums)
techila.peach(funcname = 'theheavyalgorithm', # Function that will be called on the compute nodes/ Workers
files = 'theheavyalgorithm.py', # Python-file that will be sourced on Workers
jobs = jobcount # Number of Jobs in the Project
)
答案 8 :(得分:1)
非常简单的并行处理示例是
from multiprocessing import Process
output1 = list()
output2 = list()
output3 = list()
def yourfunction():
for j in range(0, 10):
# calc individual parameter value
parameter = j * offset
# call the calculation
out1, out2, out3 = calc_stuff(parameter=parameter)
# put results into correct output list
output1.append(out1)
output2.append(out2)
output3.append(out3)
if __name__ == '__main__':
p = Process(target=pa.yourfunction, args=('bob',))
p.start()
p.join()
答案 9 :(得分:1)
假设我们有一个异步函数
async def work_async(self, student_name: str, code: str, loop):
"""
Some async function
"""
# Do some async procesing
那需要在大型阵列上运行。一些属性被传递给程序,一些属性从数组中的dictionary元素的属性中使用。
async def process_students(self, student_name: str, loop):
market = sys.argv[2]
subjects = [...] #Some large array
batchsize = 5
for i in range(0, len(subjects), batchsize):
batch = subjects[i:i+batchsize]
await asyncio.gather(*(self.work_async(student_name,
sub['Code'],
loop)
for sub in batch))
答案 10 :(得分:1)
达斯克期货;我很惊讶还没有人提到它。 . .
from dask.distributed import Client
client = Client(n_workers=8) # In this example I have 8 cores and processes (can also use threads if desired)
def my_function(i):
output = <code to execute in the for loop here>
return output
futures = []
for i in <whatever you want to loop across here>:
future = client.submit(my_function, i)
futures.append(future)
results = client.gather(futures)
client.close()
答案 11 :(得分:0)
您可以使用 asyncio 。 (可以在here中找到文档)。它被用作多个Python异步框架的基础,这些框架提供了高性能的网络和Web服务器,数据库连接库,分布式任务队列等。此外,它还具有高级和低级API来解决任何类型的问题。
import asyncio
def background(f):
def wrapped(*args, **kwargs):
return asyncio.get_event_loop().run_in_executor(None, f, *args, *kwargs)
return wrapped
@background
def your_function(argument):
#code
现在,此函数将在每次调用时并行运行,而不会使主程序进入等待状态。您也可以使用它并行化循环。当调用for循环时,尽管循环是顺序的,但是每次迭代都在解释器到达主程序后与主程序并行运行。 例如:
@background
def your_function(argument):
time.sleep(5)
print('function finished for '+str(argument))
for i in range(10):
your_function(i)
print('loop finished')
这将产生以下输出:
loop finished
function finished for 4
function finished for 8
function finished for 0
function finished for 3
function finished for 6
function finished for 2
function finished for 5
function finished for 7
function finished for 9
function finished for 1
答案 12 :(得分:0)
concurrent 的 tqdm library 包装器是并行化长时间运行代码的好方法。 tqdm 通过智能进度表提供当前进度和剩余时间的反馈,我发现这对于长时间计算非常有用。
循环可以重写为通过简单调用 SELECT
作为并发线程运行,或者通过简单调用 thread_map
作为并发多进程运行:
process_map
答案 13 :(得分:-1)
看看这个;
http://docs.python.org/library/queue.html
这可能不是正确的方法,但我会做类似的事情;
实际代码;
from multiprocessing import Process, JoinableQueue as Queue
class CustomWorker(Process):
def __init__(self,workQueue, out1,out2,out3):
Process.__init__(self)
self.input=workQueue
self.out1=out1
self.out2=out2
self.out3=out3
def run(self):
while True:
try:
value = self.input.get()
#value modifier
temp1,temp2,temp3 = self.calc_stuff(value)
self.out1.put(temp1)
self.out2.put(temp2)
self.out3.put(temp3)
self.input.task_done()
except Queue.Empty:
return
#Catch things better here
def calc_stuff(self,param):
out1 = param * 2
out2 = param * 4
out3 = param * 8
return out1,out2,out3
def Main():
inputQueue = Queue()
for i in range(10):
inputQueue.put(i)
out1 = Queue()
out2 = Queue()
out3 = Queue()
processes = []
for x in range(2):
p = CustomWorker(inputQueue,out1,out2,out3)
p.daemon = True
p.start()
processes.append(p)
inputQueue.join()
while(not out1.empty()):
print out1.get()
print out2.get()
print out3.get()
if __name__ == '__main__':
Main()
希望有所帮助。
答案 14 :(得分:-1)
感谢@iuryxavier
from multiprocessing import Pool
from multiprocessing import cpu_count
def add_1(x):
return x + 1
if __name__ == "__main__":
pool = Pool(cpu_count())
results = pool.map(add_1, range(10**12))
pool.close() # 'TERM'
pool.join() # 'KILL'