1

I have 2 processes which have the same boolean. Changes on this boolean has no effect on the other processes.

def func1():   # function wich changes the boolean
    global boolVar
    if(boolVar)
       boolVar = False
       ...

def func1():   # function wich have no effect on the change frm func1
    global boolVar
    if(boolVar)
    ...

boolVar = True # var in " if __name__ == "__main__": "

p1 = multiprocessing.Process(target=func1, args=(boolVar)) #my processes
p2 = multiprocessing.Process(target=func2, args=(boolVar))
p1.start()
p2.start()

I need that the changes from func1 effects func2. How can I do this?

martineau
  • 119,623
  • 25
  • 170
  • 301
etwasmitbaum
  • 101
  • 1
  • 7

3 Answers3

2

The memory of processes is separated from each other, not by Python but by the operating system. That's done for security reasons.

What you need is called interprocess communication. Since you already use the multiprocessing library, have a look at message passing with pipes

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • This is the right answer. Trying to share memory between multiple processes is an error-prone and risky task. – abdusco Jul 05 '19 at 22:53
  • I agree with Thomas, IPC is the correct way to pass messages here. My answer is some hacky noodling for if you wanted an alternative to IPC – garyboland Jul 05 '19 at 23:05
0
import multiprocessing
boolVar=False
def func1(_bool):   # function wich changes the boolean
    global boolVar
    if not boolVar:
       boolVar = True

def func2():   # function wich have no efffect on the change frm func1
    global boolVar
    if boolVar:
        print('worked!')


boolVar = True # var in " if __name__ == "__main__": "

p1 = multiprocessing.Process(target=func1, args=(boolVar,)) #my processes
p2 = multiprocessing.Process(target=func2)
p1.start()
p2.start()

$ python3 multi.py

worked!

garyboland
  • 135
  • 12
0

Processes run in separate memory-spaces, which means global variables cannot be shared. The documentation has a section titled Sharing state between processes that discusses some ways around this when there's a need.

Here's a runnable one of those ways applied along the lines of the pseudocode in your question. It uses an array type code of 'i' for an integer because there isn't one specifically for boolean values — but that's OK because in Python booleans are a subclass of int.

Note that the current value of Value shared memory object must be accessed through its value attribute as shown. This is because they are implemented as object proxies with an associated Lock to prevent concurrent access. The overhead of having to do this can significantly reduce any gains obtain from the multiprocessing itself depending on exactly what you're trying to do.

import multiprocessing
import time

def func1(boolVar):
    if boolVar.value:
       boolVar.value = False

def func2(boolVar):
    time.sleep(1)  # For testing, give other process time to execute.
    print(bool(boolVar.value))  # Show current "truthiness".


if __name__ == '__main__':
    boolVar = multiprocessing.Value('i', True)  # Create value in shared memory.

    p1 = multiprocessing.Process(target=func1, args=(boolVar,))
    p2 = multiprocessing.Process(target=func2, args=(boolVar,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
martineau
  • 119,623
  • 25
  • 170
  • 301