I'm trying to run multiple main() functions from a number of modules at the same time. I first tried using threading which worked, but I couldn't't find a way to terminate the processes when the parent process is killed nicely. so I moved onto the multiprocessing module. However for some reason p.start() is blocking and only the first of 3 modules in the loop start.
import os
import signal
import sys
from multiprocessing import Process
jobs = []
for root, dirs, files in os.walk('modules'):
for dir in dirs:
print dir
module = getattr(__import__("modules." + dir), dir)
func = getattr(module, "main", None)
if func:
p = Process(target=func)
jobs.append(p)
p.start()
dirs[:] = [] # don't recurse into directories.
print jobs
Output is:
limitlessLED
[<Process(Process-1, started)>]
Connected to MQTT