使用循环多处理共享变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用循环多处理共享变量相关的知识,希望对你有一定的参考价值。

function1 generate生成两个名为daydate和numbers的变量。要实现的是function2接收这些变量,打印并将它们存储在数据帧中。这个过程保持完整是非常重要的。

import random
from  multiprocessing import Process
import time
from datetime import datetime

def function1():
    while True:
        daydate = datetime.now()
        numbers = random.randrange(1,215)
        print(daydate, numbers)
        time.sleep(10)

def function2():
    while True:
        print("Recevied values from function1: (daydate, numbers)")
        time.sleep(10)

if __name__ == "__main__":
    a =Process(target=function1, args=())
    a.start() 
    b =Process(target=function2, args=())
    b.start()
    a.join()
    b.join()
答案

可能最通用的方式是使用multiprocessing.Manager,因为它可以传输listdict类型。与...不同ValueArray那些不属于一种类型。我已将代码重新编写到下面的示例中。

import random
from multiprocessing import Process, Manager
import time
from datetime import datetime


def function1(d):
    while True:
        daydate = datetime.now()
        number = random.randrange(1, 215)
        print('Sent do function2: ({}, {})'.format(daydate, number))
        d['date'] = daydate
        d['number'] = number
        time.sleep(2)


def function2(d):
    while True:
        print("Recevied values from function1: ({}, {})".format(d['date'], d['number']))
        time.sleep(2)


if __name__ == "__main__":
    with Manager() as manager:
        d = manager.dict()
        a = Process(target=function1, args=(d,))
        a.start()
        b = Process(target=function2, args=(d,))
        b.start()
        a.join()
        b.join()

这会产生以下输出:

> python3 manager.py
Sent do function2: (2017-12-31 10:52:33.405475, 80)
Recevied values from function1: (2017-12-31 10:52:33.405475, 80)
Recevied values from function1: (2017-12-31 10:52:33.405475, 80)
Sent do function2: (2017-12-31 10:52:35.466549, 71)
Sent do function2: (2017-12-31 10:52:37.566320, 138)
Recevied values from function1: (2017-12-31 10:52:35.466549, 71)
Sent do function2: (2017-12-31 10:52:39.601367, 124)
Recevied values from function1: (2017-12-31 10:52:37.566320, 138)
Sent do function2: (2017-12-31 10:52:41.626318, 183)
Recevied values from function1: (2017-12-31 10:52:39.601367, 124)

如您所见,这可以多次检索相同的值。要只获得一次值,请使用multiprocessing.Queue

import random
from multiprocessing import Process, Queue
import time
from datetime import datetime


def function1(q):
    while True:
        daydate = datetime.now()
        number = random.randrange(1, 215)
        print('Sent to function2: ({}, {})'.format(daydate, number))
        q.put((daydate, number))
        time.sleep(2)


def function2(q):
    while True:
        date, number = q.get()
        print("Recevied values from function1: ({}, {})".format(date, number))
        time.sleep(2)


if __name__ == "__main__":
    q = Queue()
    a = Process(target=function1, args=(q,))
    a.start()
    b = Process(target=function2, args=(q,))
    b.start()
    a.join()
    b.join()

这会产生以下输出:

> python3 qtest.py
Sent to function2: (2017-12-31 11:13:34.331509, 54)
Recevied values from function1: (2017-12-31 11:13:34.331509, 54)
Sent to function2: (2017-12-31 11:13:36.337707, 194)
Recevied values from function1: (2017-12-31 11:13:36.337707, 194)
Sent to function2: (2017-12-31 11:13:38.472709, 171)
Recevied values from function1: (2017-12-31 11:13:38.472709, 171)
另一答案
 Your Program is Correct it shows proper output.
 I run The program and i get OUTPUT

 (datetime.datetime(2017, 12, 31, 12, 27, 19, 930988), 168)
   Recevied values from function1: (daydate, numbers)
 (datetime.datetime(2017, 12, 31, 12, 27, 29, 941160), 12)
   Recevied values from function1: (daydate, numbers)
 (datetime.datetime(2017, 12, 31, 12, 27, 39, 951035), 21)
   Recevied values from function1: (daydate, numbers)
 (datetime.datetime(2017, 12, 31, 12, 27, 49, 958898), 191)
   Recevied values from function1: (daydate, numbers)
 (datetime.datetime(2017, 12, 31, 12, 27, 59, 963084), 118)
   Recevied values from function1: (daydate, numbers)
 (datetime.datetime(2017, 12, 31, 12, 28, 9, 971400), 194)
   Recevied values from function1: (daydate, numbers)
 (datetime.datetime(2017, 12, 31, 12, 28, 19, 978968), 170)
   Recevied values from function1: (daydate, numbers)
 (datetime.datetime(2017, 12, 31, 12, 28, 29, 986960), 40)
   Recevied values from function1: (daydate, numbers)
 (datetime.datetime(2017, 12, 31, 12, 28, 39, 995139), 94)

以上是关于使用循环多处理共享变量的主要内容,如果未能解决你的问题,请参考以下文章

在 python 多处理中传递共享内存变量

python多处理中的共享变量

java多线程执行任务,处理共享成员变量的安全问题

多线程下处理变量共享的几种方式

Python多处理:如何将共享变量用于复杂类的列表?

python多线程