I would like to run two functions at the same time. One of the functions has to access the created variables of the other one.
What I’ve done so far:
import multiprocessing import Process
import sys
import time
def func1():
x = 0
while True:
x += 1
time.sleep(1)
return x
def func2():
y = 0
while True:
y = x
print(y)
time.sleep(1)
return y
if __name__=='__main__':
p1 = Process(target = func1())
p1.start()
p2 = Process(target = func2())
p2.start()
p1.join()
p2.join()
The functions will be executed both but the x won’t be recognized in the second function and instead is just printed y = 0.
How can I use the current value of my variable x in function 2?
In my case, I have to use a GPShat of my raspberry pi which needs a couple of seconds to be read. Independently of my this GPS function I would like to run function2 and while executing it, I take the current GPS data every loop.
Thanks 🙂
-
1I have answered but the question is really off-topic for this site. – joan 13 hours ago
-
Hi @Philipp, Welcome and nice to meet you. Now let me see. So you have two functions running as two processes using say python 3.8.1 package: “Python 3.8.1 > Standard Library > Concurrent Execution > Multiprocessing” : docs.python.org/3/library/multiprocessing.html. Now let me see if I understand you problem. (1) You have two functions, say (a) Function A getting GPS “data” every couple of minutes, and “pushes” the “data” in a “box”. – tlfong01 11 hours ago
-
(b) Function B which continuously checks if there is any new data, and if yes, “pulls” new data out of the “box”, and does something, such as printing data out, or moving a servo etc. You may like to correct me or modify my story, before I move on. – tlfong01 11 hours ago
-
I quickly read your program and found the first and last section look OK. Now func1() returns x = 1, 2, 3, 4, … However, func2() does not read y = 1, 2, 3, 4. As suggested by @joan, if x, y are main program’s global variables, and you declare x, y as global in the definitions of fun1() and fun(2). Your problem might be solved. But there is a “sync” problem, say fun1() is returning x too fast, so funB() might miss some, … – tlfong01 11 hours ago
-
If the GPS data are simple text messages, I think you can use the “pipe” or “queue” thing to let func1() send messages at one end of a pipe, and func2() receives messages at the other end, and nothing will be missed. But if your GPS data is a more complicated data structure such as a list or or dictionary/table, then perhaps you need a “database/dictionary/table” with a “LOCK”. 🙂 – tlfong01 11 hours ago
-
I googled further and found two more things you can try: (1) A “shared memory map” using “Value” or “Array”, (2) “Server process” holding “Manager()” objects such as list, dict. I have not tried these two more complicated things. But if you think they might be useful, I will try to explore. – tlfong01 10 hours ago
-
Actually I have a long stalled GPSD project, and perhaps I can try your multiprocessing functions to play with my collected GPS data in the form of complicated tables and images. raspberrypi.stackexchange.com/questions/98840/…. Cheers. – tlfong01 10 hours ago
Variables declared in a function are only accessible within that function and cease to exist afterwards.
If you want to share a variable declare it in module scope and mark it global in each function you want to use it within.
E.g.
shared = 0
def func_1():
global shared
shared = shared + 1
print ("func_1 shared=", shared)
def func_2():
global shared
shared = shared + 51
print ("func_2 shared=", shared)
It is your responsibility to ensure overlapping changes to the global are safe.
Categories: Uncategorized