Multiprocessing: no output from third process
Context: I have 3 files: 2 of which collect data every 10 seconds and one of which should be collecting this data for machine learning. I've set up multiprocessing where I have a function for each file that contains making an instance of its class and calls the necessary functions, then in the main making 3 processes, starting, and joining them where the third process only starts and joins every 10 seconds since that is when data comes in and when it is needed.
import gui_and_keyboard_features
import brain_features
import machine_learning
import multiprocessing as mp
import sys
import time
# increase recursion limit
sys.setrecursionlimit(15000)
def first_file():
gui1 = gui_and_keyboard_features.gui()
gui1.realtime()
gui1.every_5_min()
gui1.main_window.mainloop()
def second_file():
myBoard = brain_features.braindata(-1, 'COM3')
myBoard.startStream()
myBoard.collectData()
# print(myBoard.compressed_brain_training_features)
def third_file():
# machine learning related
myml = machine_learning.ml()
myml.add_raw_data()
# myml.add_training_data()
# myml.train_model()
# myml.predict()
# keyboard related
ml_keyboard_data = gui_and_keyboard_features.gui()
ml_keyboard_data.realtime()
ml_keyboard_data.every_5_min()
ml_keyboard_data.main_window.mainloop()
# brain related
ml_brain_data = brain_features.braindata()
ml_brain_data.startStream()
ml_brain_data.collectData()
if __name__ == "__main__":
# add ml file code here
start_time = time.time()
proc1 = mp.Process(target=first_file)
proc2 = mp.Process(target=second_file)
proc3 = mp.Process(target=third_file)
proc1.start()
proc2.start()
proc1.join()
proc2.join()
print("we are here")
while True:
if (int(time.time() - start_time) % 10 == 0.0) and (int(time.time() - start_time) != 0.0):
proc3.start()
proc3.join()
print("finished running")
Problem: when running the multiprocessing file, I only get output from the 2 data files and nothing from the 3rd machine learning file. There are no while loops contained in the 2 data files, but one file is connected to a gui made with tkinter where a .after() function that takes a time interval and function and continuously reruns the function after that time interval. I have set up print statements where these .after() functions occur in the first data file as well as in the second data and machine learning files. When this is run through the multiprocessing file, it correctly loops through these data files but it never reaches the print statement contained in the machine learning file.
self.main_window.after(9500, self.realtime)
self.main_window.after(10000, self.every_5_min)
every 10s keyboard features
every 5 min keyboard features
brain features
every 10s keyboard features
every 5 min keyboard features
brain features
every 10s keyboard features
every 5 min keyboard features
brain features
every 10s keyboard features
every 5 min keyboard features
brain features
Solution 1:
Actually your code even stops before reaching your while loop
. Or is "we are here"
ever printed? Using join
on process 1 and 2 is waiting until these processes are terminated. But because you have infinite processes 1 and 2 your script will never continue. Try to run it without the join
statements.
Also you might want to implement proc1.daemon = True
before you call proc1.start()
(same for proc2) to ensure the subprocesses are killed once the main process stops.