Whilst looking at the python doc for multiprocessing.terminate()
I came across the following:
Terminate() If this method is used when the associated process is using a pipe or queue then the pipe or queue is liable to become corrupted and may become unusable by other process. Similarly, if the process has acquired a lock or semaphore etc. then terminating it is liable to cause other processes to deadlock.
Which basically says if you terminate a process which is using a queue, pipe or similar you run the risk of the structure becoming corrupt.
I have a couple of questions about this,
I understand you should always try to not use terminate, but this is for that situation where you cannot do anything else but this
Close a Python Multiprocessing Queue If you want no process should write into a multiprocessing queue, you can close the queue using the close() method. The close() method, when invoked on a multiprocessing queue in any of the processes, closes the queue.
A queue is a data structure on which items can be added by a call to put() and from which items can be retrieved by a call to get(). The multiprocessing. Queue provides a first-in, first-out FIFO queue, which means that the items are retrieved from the queue in the order they were added.
Terminating processes in Python We can kill or terminate a process immediately by using the terminate() method. We will use this method to terminate the child process, which has been created with the help of function, immediately before completing its execution.
Simply use q = ClearableQueue() in all places where you used q = Queue() , and call q. clear() when you'd like.
You could add checksums to the blocks of data you pass around and check them to confirm no data corruption occurred. This is a common technique in any data communication that has a risk of data corruption. You could look at hashlib for this and use something like md5 or crc32 checksums.
Okay, so it was not the nicest solution but I have firstly handled the exceptions as best as possible. I did not want to run the risk of having corruption so I now restart the application in an instance where there is the possibility of a corruption occurring to reduce the chance of possible issues.
Thanks @MarwanAlsabbagh for your suggestion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With