Problem description: I am working with the simulator to extract some dataset from it. The idea is to run multiple processes to perform various task. For example: moving the vehicle using one process and data collection using another process. In the data collection process, 3 threads are running to record three different data types and the recording has to occur periodically. Also, the data should be recorded synchronously.
Sample code is provided without details.
import threading
import multiprocessing
import time
class DataRecorder:
def __init__(self):
"""
some parameters
"""
pass
def move_vehicle(self, path):
pass
def record_data1(self):
pass
def record_data2(self):
pass
def record_data3(self):
pass
def record_data():
t1 = threading.Thread(target=self.record_data1)
t2 = threading.Thread(target=self.record_data2)
t3 = threading.Thread(target=self.record_data3)
threads = [t1, t2, t3]
for thread in threads:
thread.start()
while (True):
for thread in threads:
if not thread.is_alive()
thread.start() # leads to threads can only be started once
for thread in threads:
if thread.is_alive()
thread.join()
time.sleep(1)
def stop_recording(self, p1):
if p1.is_alive():
p1.terminate()
def move_and_record():
P1 = multiprocessing.Process(target=self.record_data)
P1.start()
self.move_vehicle(path)
self.stop_recording(P1)
The problem:
RuntimeError: threads can only be started once.
And inside while loop, the threads gets stopped after 1st iteration. I have tried both without .join() part and with .join() part.
I am also looking for another alternative to solve this problem.