I'm using multiprocessing on Python 3.6 on Ubuntu to handle the faster communication with another device.
I set daemon = True
to terminate the child process when the parent process finishes. However, when the main process is terminated, the another process (_another_process in the following code) sometimes isn't terminated and continues to be alive. Then, when I run the same program again, I get address already in use
error when I run the above code. Of course, I can kill this process, but it's annoying and I'd like to solve.
Class Xxx
def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.settimeout(2.5)
self.sock.bind((self.ip, self.port))
self.sock.settimeout(None)
self.start_process()
time.sleep(1.5)
def start_process(self):
p = mp.Process(target=self._another_process)
time.sleep(1)
p.daemon = True
p.start()
def _another_process(self):
while True:
# Do continuous (infinite) operation
I don't know why sometimes terminated and sometimes not, but are there any better implementations to realize what I want? Or, is daemon = True
the best way?
I believe I shouldn't use join()
because my child process has an infinite operation, but if I misunderstood, please let me know.