0

I am using a Manager from pythons multiprocessing library. I would like to share variables between threads.

So I have a function and this function uses one such variable internally. Because of python's multiprocessing rules, I need to put the Manager and the variables in if __name__ == '__main__':. Is there a smart way to still access the variable status? Code below:

from multiprocessing import Value, Manager

def first_methode():
    if status.value:
        do_something...

if __name__ == '__main__':
    manager = Manager()
    recording_status = manager.Value(status, False)
    thread = multiprocessing.Process(target=first_methode)

This gives me a

NameError: name 'status' is not defined

Any suggestions? Thanks!

1 Answers1

2

You're not defining variable status anywhere so you can't access it whether it's multiprocessing or not. You probably meant manager.Value("status", False), i.e. the literal string "status", not a variable called status.

Additionally, take a look at examples in the docs and perhaps the code in here as several other things in your code are off, for example the Value object you create should be passed to the subprocess/function as an argument. Otherwise code in the function has no way of telling what status is because it's not defined in the function's scope.

Czaporka
  • 2,190
  • 3
  • 10
  • 23