1

Here is the example I have created. Essentially, I define two processes as global variables (yes, I know that's bad practice but I'm doing it to show the problem). The target of the killer process is the kill_printer function, which terminates the printer process after 5 seconds. Both processes are global objects so this should not be a problem.

from time import sleep
from multiprocessing import Process

def kill_printer():
    print('printer' in globals() and 'killer' in globals())
    sleep(5)
    printer.terminate()

def print_hello():
    while True:
        print('hello')
        sleep(1)

if __name__ == '__main__':
    global printer
    global killer
    printer = Process(target=print_hello)
    killer = Process(target=kill_printer)
    printer.start()
    killer.start()
    print('printer' in globals() and 'killer' in globals())

However, as you can see I have printed tests to confirm that printer and killer are both global variables after their definition and when printer is needed in kill_printer. This is because when the program is run, this is the result:

True
hello
False
hello
hello
hello
hello
hello
Process Process-2:
Traceback (most recent call last):
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35\lib\multiprocessing\process.py", line 254, in _bootstrap
    self.run()
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Alex\Google Drive\EES\test.py", line 7, in kill_printer
    printer.terminate()
NameError: name 'printer' is not defined
hello
hello
hello
hello

One minute both Processes are in globals, and suddenly they're not (causing a NameError, and printer goes right on printing! What's happening here? Is this problem specific to my use of processes or variables themselves in general?

(If anyone's about to reply that multiprocessing isn't necessary here, they should know that the given example is a reduced down example of a program that most definitely does)

anon582847382
  • 19,907
  • 5
  • 54
  • 57
  • Are you on Windows? It looks like `printer` and `killer` are in the original process's globals, but not the `killer` process's globals. That'd happen on Windows, since it doesn't have fork. – user2357112 Nov 05 '15 at 16:59
  • @user2357112 I am indeed on windows. I have access to a computer with Linux, are you saying it would work on that? – anon582847382 Nov 05 '15 at 17:00
  • What are you trying to achieve? – Peter Wood Nov 05 '15 at 17:02
  • 2
    Don't use globals, if you want to send information to other threads use a Queue, and pass args to the target – Chad S. Nov 05 '15 at 17:03
  • 1
    I'd say, the error message comes from the *killer* process, which does not inherit the global variables *at all* (neither does the printer process, but it also does not try access them). You did expect the globals to be shared? [They are not](https://stackoverflow.com/questions/11215554/globals-variables-and-python-multiprocessing). – dhke Nov 05 '15 at 17:03
  • @ChadS. OK, how would I apply that to my example? – anon582847382 Nov 05 '15 at 17:05
  • @dhke Ah, I see. That makes perfect sense. – anon582847382 Nov 05 '15 at 17:05
  • As a side note: Life probably becomes a lot simpler when you start the printer from a monitor process and kill it from there, because then you've got access to the process object of the forked-off printer directly. – dhke Nov 05 '15 at 17:07
  • And the lesson here is: `Use the *threading* module when on the one single non-POSIX-compliant platform, otherwise known as M$ Windows` ;). – 3442 Nov 05 '15 at 17:07
  • @ChadS. I will quite happily accept an answer building on your comments that provides a solution to my example. – anon582847382 Nov 05 '15 at 17:26

0 Answers0