Reputation: 318
I'm making use of different processes but I'm unable to make use of global variable. I have 2 processes, 1 how gather data from an api and the last process uses this data and does some calculations on the obtained data. I've followed this article but it doesn't work for me. Can someone help me?
import pandas as pd
import numpy as np
import multiprocessing
import time
class Example():
def __init__(self):
manager = multiprocessing.Manager()
self.curValues1 = manager.dict()
def main(self):
p1 = multiprocessing.Process(target=self.gatherProcess1)
p2 = multiprocessing.Process(target=self.calc)
p1.start()
p2.start()
p1.join()
p2.join()
def gatherProcess1(self):
df = pd.DataFrame([['tom', 10], ['nick', 15], ['juli', 14]], columns = ['Name', 'Age']) # Get data from api
self.curValues1 = {"lastName": df["Name"].iloc[-1], "lastAge": df["Age"].iloc[-1]}
def calc(self):
time.sleep(1)
print(self.curValues1["lastName"], self.curValues1["lastAge"])
example = Example()
example.main()
Upvotes: 0
Views: 43
Reputation: 94881
You have to update self.curValues1
dict in place, not re-assign it:
self.curvalues1.update({"lastName": df["Name"].iloc[-1], "lastAge": df["Age"].iloc[-1]})
When you re-assign self.curValues1
the way you currently do, you just throw away the multiprocessing.Manager
dict
and assign it to a regular dict
.
Upvotes: 2