Skip to content Skip to sidebar Skip to footer

Python 3 - Multiprocessing - Queue.get() Does Not Respond

I want to make a brute force attack and therefore need some speed... So I came up to use the multiprocessing library... However, in every tutorial I've found, something did not wor

Solution 1:

@dano hit it on the head! You don't have if __name__ == "__main__": so you have a "fork bomb". That is, each process is running the processes, and so on. You will also notice that I have moved the creation of the queue.

import multiprocessing as mp
import random
import string


# define a example functiondefrand_string(length, output):
    """ Generates a random string of numbers, lower- and uppercase chars. """
    rand_str = ''.join(random.choice(
                string.ascii_lowercase
                + string.ascii_uppercase
                + string.digits)
    for i inrange(length))
        output.put(rand_str)


 if __name__ == "__main__":
     # Define an output queue
     output = mp.Queue()

     # Setup a list of processes that we want to run
     processes = [mp.Process(target=rand_string, args=(5, output)) for x inrange(2)]

     # Run processesfor p in processes:
        p.start()

    # Exit the completed processesfor p in processes:
        p.join()

    # Get process results from the output queue
    results = [output.get() for p in processes]

    print(results)  

What happens is that multiprocessing runs each child process as a module, so __name__ is only __main__ in the parent. If you don't have that then each child process will (attempt to) start two more processes, each of which will start two more, and so on. No wonder IDLE stops.

Post a Comment for "Python 3 - Multiprocessing - Queue.get() Does Not Respond"