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)