0

I am using @anonymous_ghoster example at How to signal slots in a GUI from a different process? to implement multiprocessing.

My code have:

class ChildProc(Process):
    def __init__(self, to_emitter: Pipe, from_mother: Queue, daemon=True):
        super().__init__()
        self.daemon = daemon
        self.to_emitter = to_emitter
        self.data_from_mother = from_mother

    def run(self):
        while True:
            text = self.data_from_mother.get()
            print(text)
            if(text[0] == "dostuff"):
                dostuff(self.to_emitter)
            if(text[0] == "dostuff" and text[1]=="stop"):
                # until dostuff() is running, this will not hit.
                stop_do_stuff()

def dostuff(conn):
    while True:
        # Somehow I want to be able to return from this function if requested by parent_process

The question I have is, how do I return from dostuff function if the parent process send something like self.process_queue.put(["dostuff", "stop"])

pefile
  • 69
  • 6
  • You have to use some form of interprocess communication, such as a pipe. – Barmar Apr 06 '22 at 20:12
  • @Barmar I am using pipe – pefile Apr 06 '22 at 20:16
  • You *can't* do this, with your current program structure - once `dostuff()` has been called, `run()` is no longer running, and cannot respond to the "stop" message. Either `dostuff()` has to check the queue for messages itself, or it needs to be run in a separate thread so that `run()` can continue receiving messages. – jasonharper Apr 06 '22 at 20:35
  • @jasonharper how do I look for message in pipe without waiting in dostuff()? – pefile Apr 06 '22 at 21:02
  • You can use `.get_nowait()` to check for a message in the Queue without waiting. – jasonharper Apr 06 '22 at 21:07
  • @jasonharper I am getting `AttributeError: 'PipeConnection' object has no attribute 'get_nowait'` – pefile Apr 06 '22 at 22:18

0 Answers0