Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mulitprocessing, terminate and corrupt queues

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,

  1. What will happen to another process trying to retrieve the data from the PIPE, Queue or similar if a corruption occurs?
  2. How can a process check to see if there is corruption?
  3. Can the deadlock be resolved in any way if you know another process has been terminated?

I understand you should always try to not use terminate, but this is for that situation where you cannot do anything else but this

like image 908
Matt Seymour Avatar asked Nov 14 '12 10:11

Matt Seymour


People also ask

How do I close a multiprocessing queue?

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.

What is a multiprocessing 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.

How do you stop a multiprocessing process?

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.

How do you clear a multiprocessing queue in Python?

Simply use q = ClearableQueue() in all places where you used q = Queue() , and call q. clear() when you'd like.


2 Answers

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.

like image 114
Marwan Alsabbagh Avatar answered Oct 27 '22 22:10

Marwan Alsabbagh


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.

like image 33
Matt Seymour Avatar answered Oct 27 '22 21:10

Matt Seymour