0

function1 generates generates two variables called daydate and numbers. What is to be achieved is that function2 receives this variables, prints and store them in a dataframe. It is very important that the process stays intact.

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 Answers 2

1

Probably the most versatile way is to use a multiprocessing.Manager because it can transfer list and dict types. And unlike e.g. Value or Array those are not bound to one type. I have re-worked your code into the example below.

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()

This produces the following output:

> 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)

As you can see, this can retrieve the same value multiple times. To just receive the value once, use a 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()

This produces the following output:

> 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)
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.