Python multiprocessing.Queue deadlocks on put and get

后端 未结 2 1501
清酒与你
清酒与你 2021-01-03 00:37

I\'m having deadlock problems with this piece of code:


def _entropy_split_parallel(data_train, answers_train, weights):
    CPUS = 1 #multiprocessing.cpu_co         


        
相关标签:
2条回答
  • I think the problem is the parent thread joining a child thread to which it has passed a Queue. This is discussed the the multiprocessing module's programming guidelines section.

    At any rate, I encountered the same symptom that you describe, and when I refactored my logic so that the master thread did not join the child threads, there was no deadlock. My refactored logic involved knowing the number of items that I should get from the results or "done" queue (which can be predicted based on either the number of child threads or the number of items on the work queue, etc.), and looping infinitely till all of these were collected.

    "Toy" illustration of the logic:

    num_items_expected = figure_it_out(work_queue, num_threads)
    items_received = []
    while len(items_received) < num_items_expected:
        items_received.append(done_queue.get())
        time.sleep(5)
    

    The above logic avoids the need for the parent thread to join the child thread, yet allows the parent thread to block until all the children are done. This approach avoided my deadlock problems.

    0 讨论(0)
  • 2021-01-03 01:21

    This problem went away with newer versions of Python, so I'm assuming it was a problem with the backport. Anyways, it's no longer an issue.

    0 讨论(0)
提交回复
热议问题