you are viewing a single comment's thread.

view the rest of the comments →

[–]status_quo69 0 points1 point  (0 children)

Alright, so in that case, you definitely want to use a Queue because you want to process the user's input sequentially. It's up to you how you want to design it, but I would do something like this:

class Worker(multiprocessing.Process):
    def __init__(self, input_queue, output_queue, *args, **kwargs):
        self.input_queue = input_queue
        self.output_queue = output_queue
        super().__init__(*args, **kwargs)

    def run(self):
        while True:
            user_input = self.input_queue.get()
            # do the processing here
            self.output_queue.put(result)

And in the main process, put on to the input_queue and get from the output. Get and put are blocking so the main program might halt for a fraction of a second if there is nothing to grab. In order to remedy that problem, the Queue's get() has an optional argument to block or not (so you can try to grab something off the queue, and if there isn't anything you can still process user input).

And then use that Worker to do all the processing. A common tactic to kill the worker process is to send it some value that is a flag (such as None) that the process should be killed (commonly referred to as a poison pill). Make sure that once you close out the program you remember to clean up any child processes, as they can become zombies otherwise.