Search code examples
pythonmultiprocessingterminate

Stop multiprocessing thread from another module


I want to kick off an infinite loop in a multiprocess in module B from module A. At a later point, I want to terminate the multiprocess from module A as well.

The problem I am having is that if I try to save a boolean like keep_running in B, it keeps getting reset, even when I tried using global. I have read that global is best avoided.

A

import multiprocessing

keep_running = True

def stop_it(conf):
   global keep_running
   keep_running = False

def start_it(arg):
   while keep_running:
       do_stuff(arg)
       time.sleep(1)

def main(args):
   ...
   configs.configuartions.Configurations()
   configs.process = multiprocessing.Process(target=start_it, args=(arg,))
   configs.process.start()

if __name__ == 'main':
   import sys
   main(sys.argv[1:])

B

import A.main as m


def on_init(<args>):
   m.main([conf_file, database])

def on_stop(conf):
   m.stop_it(conf)

What is the best way to accomplish this?


Solution

  • If you are using multiprocessing, you have created separate processes that cannot access each other's variables. In such case you have to communicate through pipes, signals, sockets or flag files.

    To be able to access each other variables, you would use threading instead of multiprocessing. However, you can still kill a different process using os.kill by sending a SIGTERM or SIGKILL to it. You would need PID for this, you can get it as configs.process.pid in your example.

    More information is in official documentation: https://docs.python.org/3/library/multiprocessing.html