使用循环多处理共享变量

时间:2017-12-31 07:13:18

标签: python multiprocessing

function1生成生成两个名为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()

2 个答案:

答案 0 :(得分:1)

可能最通用的方法是使用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)

答案 1 :(得分:0)

 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)