简体   繁体   English

进程池中的共享内存

[英]Shared memory in pool of processes

I'm trying to share memory between 4 processes but I it is not working for me, I would like to get help with my problem.我正在尝试在 4 个进程之间共享内存,但它对我不起作用,我想就我的问题寻求帮助。

Take a look at the logic of the flow:看一下流程的逻辑:

from multiprocessing import Pool, Value
from time import sleep

def work(process):
    if process == 'A':
        sleep(secs=2)     # Processing task A
        a = True          # Marking a shared byte of completed task
    if process == 'B':
        sleep(secs=1)     # Starting processing task B
        while a is False: # Waiting until task A will complete
            pass
        sleep(secs=5)     # Continuing processing task B
        b = True          # Marking a shared byte of completed task
    if process == 'C':
        sleep(secs=7)     # Processing task C
    if process == 'D':
        sleep(secs=1)     # Starting processing task D
        while b is False: # Waiting until task B will complete
            pass
        sleep(secs=4)     # Continuing processing task D

def main():
    a = Value('b', False)
    b = Value('b', False)

    with Pool(processes=4) as pool:
        pool.map(func=work, iterable=('A', 'B', 'C', 'D'))

if __name__ == '__main__':
    main()

I can't share the values a and b , how should I do that?我不能共享值ab ,我该怎么做?

Thank a lot!非常感谢!

First, function sleep does not take a keyword secs= parameter.首先,功能sleep采取关键字secs=参数。 Second, you need to pass values a and b to your function work as arguments.其次,您需要将值传递ab给你的函数work作为参数。 Third, you need to set and test the value attribute of you Value instances.第三,您需要设置和测试您的Value实例的value属性。 Fourth, since you are passing these arguments to pool processes, you should get the Value instances from a SyncManager instance as shown below.第四,由于您将这些参数传递给池进程,您应该从SyncManager实例中获取Value实例,如下所示。

from multiprocessing import Pool, Manager
from time import sleep
from functools import partial

def work(a, b, process):
    if process == 'A':
        sleep(2)     # Processing task A
        print('A is marking a True')
        a.value = True          # Marking a shared byte of completed task
    elif process == 'B':
        sleep(1)     # Starting processing task B
        while a.value is False: # Waiting until task A will complete
            pass
        sleep(5)     # Continuing processing task B
        print('B is marking b True')
        b.value = True          # Marking a shared byte of completed task
    elif process == 'C':
        sleep(7)     # Processing task C
    elif process == 'D':
        sleep(1)     # Starting processing task D
        while b.value is False: # Waiting until task B will complete
            pass
        print('D has now found b to be True')
        sleep(4)     # Continuing processing task D

def main():
    manager = Manager()
    a = manager.Value('b', False)
    b = manager.Value('b', False)

    with Pool(processes=4) as pool:
        pool.map(func=partial(work, a, b), iterable=('A', 'B', 'C', 'D'))

if __name__ == '__main__':
    main()

Prints:印刷:

A is marking a True
B is marking b True
D has now found b to be True

You can use a Queue (multiprocessing.Queue() ) to pass messages back and forth.您可以使用队列(multiprocessing.Queue())来回传递消息。

See this link https://pymotw.com/2/multiprocessing/communication.html请参阅此链接https://pymotw.com/2/multiprocessing/communication.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM