[英]Shared variable in concurrent.futures.ProcessPoolExecutor() python
I want to use parallel to update global variable using module concurrent.futures in python我想使用并行更新全局变量,使用 python 中的模块 concurrent.futures
It turned out that using ThreadPoolExecutor can update my global variable but the CPU did not use all their potential (always at 5-10%), which is so slow事实证明,使用 ThreadPoolExecutor 可以更新我的全局变量,但 CPU 并没有充分利用它们的潜力(总是在 5-10%),这太慢了
and ProcessPoolExecutor can use all the cores (at 100%) but my global variable can not be updated because they do not share the same global variable和 ProcessPoolExecutor 可以使用所有核心(100%),但我的全局变量无法更新,因为它们不共享相同的全局变量
How can I share my global variable using ProcessPoolExecutor in concurrent.futures model. Thank you a lot for your help如何在 concurrent.futures model 中使用 ProcessPoolExecutor 共享我的全局变量。非常感谢您的帮助
Process doesn't seem like thread that using same memory space.进程看起来不像使用相同 memory 空间的线程。 So you need some special way to update variables.
所以你需要一些特殊的方法来更新变量。
ProcessPoolExecutor
uses the multiprocessing
module, the are two ways for sharing data, Shared memory and Server process. ProcessPoolExecutor
使用了multiprocessing
模块,共享数据有Shared memory和Server process两种方式。 First way using shared memory map, Server process using Manager
object that like a proxy to holds sharing data.第一种方式使用共享 memory map,服务器进程使用
Manager
object 像代理一样保存共享数据。 Server process are more flexible, Shared memory more efficient.服务器进程更灵活,共享memory更高效。
Using Server process sharing data like ThreadPoolExecutor
, just pass arguments to you function.使用服务器进程共享数据,如
ThreadPoolExecutor
,只需将 arguments 传递给您 function。
def running_proxy(mval):
# consider lock if you need
return mval.value
def start_executor():
with multiprocessing.Manager() as manager:
executor = ProcessPoolExecutor(max_workers=5)
mval = manager.Value('b', 1)
futures = [executor.submit(running_proxy, mval) for _ in range(5)]
results = [x.result() for x in futures]
executor.shutdown()
But Shared memory way has some difference, you need setting shared variable to global.但是Shared memory方式有些区别,需要将shared变量设置为全局。
def running_shared():
# consider lock if you need
return sval.value
def set_global(args):
global sval
sval = args
def start_executor():
sval = multiprocessing.Value('b', 1)
# for 3.7+
executor = ProcessPoolExecutor(max_workers=5, initializer=set_global, initargs=(sval,))
# for ~3.6
# set_global(sval)
# executor = ProcessPoolExecutor(max_workers=5)
futures = [executor.submit(running_shared) for _ in range(5)]
results = [x.result() for x in futures]
executor.shutdown()
I want to use parallel to update global variable using module concurrent.futures in python我想在 python 中使用模块 concurrent.futures 使用并行更新全局变量
It turned out that using ThreadPoolExecutor can update my global variable but the CPU did not use all their potential (always at 5-10%), which is so slow事实证明,使用 ThreadPoolExecutor 可以更新我的全局变量,但 CPU 并没有充分利用它们的潜力(总是在 5-10%),这太慢了
and ProcessPoolExecutor can use all the cores (at 100%) but my global variable can not be updated because they do not share the same global variable和 ProcessPoolExecutor 可以使用所有核心(100%)但我的全局变量无法更新,因为它们不共享相同的全局变量
How can I share my global variable using ProcessPoolExecutor in concurrent.futures model.如何在 concurrent.futures model 中使用 ProcessPoolExecutor 共享我的全局变量。 Thank you a lot for your help
非常感谢您的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.