multiprocessing and gpsd notes

Active today
Viewed 14 times

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
  return x

def func2():
  y = 0
  while True:
    y = x
  return y

if __name__=='__main__':
    p1 = Process(target = func1())
    p2 = Process(target = func2())

The functions will be executed both but the x wont’t be recognised in the second function and instead is 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 gps hat of my raspberry pi which needs a couple of seconds to be read. Independendly of my this gps function I would like to run function2 and while executing it, I take the current gps data every loop.

Thanks 🙂

  • I have answered but the question is really off-topic for this site. – joan 3 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 48 mins 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 46 mins 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 28 mins 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 21 mins 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 6 mins 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 2 mins ago    Edit   

2 Answers


Processes don’t share globals. Have a look at this: Exchanging objects between processes

  • I can’t be bothered to write a test but I’m fairly sure globals still work. – joan 1 hour 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.


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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: