0

Below is the code which demonstrates the problem. Please note that this is only an example, I am using the same logic in a more complicated application, where I can't use sleep as the amount of time, it will take for process1 to modify the variable, is dependent on the speed of the internet connection.

from multiprocessing import Process

code = False

def func():
    global code
    code = True

pro = Process(target=func)
pro.start()

while code == False:
    pass

pro.terminate()
pro.join()

print('Done!')

On running this nothing appears on the screen. When I terminate the program, by pressing CTRL-C, the stack trace shows that the while loop was being executed.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Abhishek Kumar
  • 298
  • 4
  • 10
  • 4
    Processes aren't threads. That process is setting its own, independent global variable. If you want to communicate between `multiprocessing` processes, use the tools `multiprocessing` provides and documents. – user2357112 Feb 19 '17 at 05:34
  • @user2357112 Could this question be marked as duplicate for: [Shared variable in python's multiprocessing](http://stackoverflow.com/questions/17377426/shared-variable-in-pythons-multiprocessing)? I need second opinion – Moinuddin Quadri Feb 19 '17 at 05:39
  • @user2357112 Can you please give an example code? – Abhishek Kumar Feb 19 '17 at 05:43
  • 1
    @AbhishekKumar: There's plenty of example code in the [docs](https://docs.python.org/3/library/multiprocessing.html). – user2357112 Feb 19 '17 at 05:45
  • 2
    @MoinuddinQuadri: That one doesn't really say anything about what happens if you try to share globals without a `Manager`. Something like [this](http://stackoverflow.com/questions/10845782/multiprocessing-module-in-python2-7-causing-some-issue) or [this](http://stackoverflow.com/questions/21525460/cant-modify-global-dict-variable-using-mutliprocessing-in-python) might work better, although I'd like to find something with a more thorough explanation of the implications of the `multiprocessing` implementation. – user2357112 Feb 19 '17 at 05:51

1 Answers1

1

Python has a few concurrency libraries: threading, multiprocessing and asyncio (and more).

multiprocessing is a library which uses subprocesses to bypass python's inability to concurrently run CPU intensive tasks. To share variables between different multiprocessing.Processes, create them via a multiprocessing.Manager() instance. For example:

import multiprocessing

import time


def func(event):
    print("> func()")
    time.sleep(1)
    print("setting event")
    event.set()
    time.sleep(1)
    print("< func()")


def main():
    print("In main()")

    manager = multiprocessing.Manager()
    event = manager.Event()
    p = multiprocessing.Process(target=func, args=(event,))
    p.start()

    while not event.is_set():
        print("waiting...")
        time.sleep(0.2)

    print("OK! joining func()...")
    p.join()

    print('Done!')


if __name__ == "__main__":
    main()
Udi
  • 29,222
  • 9
  • 96
  • 129