简体   繁体   English

concurrent.futures.ProcessPoolExecutor() 中的共享变量 python

[英]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.

相关问题 如何在 Python 中的 concurrent.futures.ProcessPoolExecutor 中传递“锁”? - How to pass the “lock” in my concurrent.futures.ProcessPoolExecutor in Python? concurrent.futures.ProcessPoolExecutor() 映射无法读取全局变量 - concurrent.futures.ProcessPoolExecutor() map can't read global variable python concurrent.futures.ProcessPoolExecutor:.submit()vs .map()的性能 - python concurrent.futures.ProcessPoolExecutor: Performance of .submit() vs .map() Python concurrent.futures.ProcessPoolExecutor:大量 RAM 用于大量任务 - Python concurrent.futures.ProcessPoolExecutor: Lot of RAM for large number of tasks Python 3.8 - concurrent.futures.ProcessPoolExecutor 性能及时下降 - Python 3.8 - concurrent.futures.ProcessPoolExecutor performance going down in time 如何监控python的concurrent.futures.ProcessPoolExecutor? - How to monitor python's concurrent.futures.ProcessPoolExecutor? 将带有对象的 function 传递到 concurrent.futures.ProcessPoolExecutor()? - Pass function with objects into concurrent.futures.ProcessPoolExecutor()? 使用初始化启动 concurrent.futures.ProcessPoolExecutor? - Launch concurrent.futures.ProcessPoolExecutor with initialization? 为什么 concurrent.futures.ProcessPoolExecutor() 跳过迭代? - Why is concurrent.futures.ProcessPoolExecutor() skipping iterations? 如何使用 asyncio 和 concurrent.futures.ProcessPoolExecutor 在 Python 中终止长时间运行的计算(CPU 绑定任务)? - How to terminate long-running computation (CPU bound task) in Python using asyncio and concurrent.futures.ProcessPoolExecutor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM