Python join a process without blocking parent

后端 未结 5 1430
孤独总比滥情好
孤独总比滥情好 2020-12-08 16:23

I\'m writing a program that will watch a particular directory for new files containing download URLs. Once a new file is detected, it will create a new process to do the act

5条回答
  •  情话喂你
    2020-12-08 16:50

    You can set up a separate thread which does the joining. Have it listen on a queue into which you push the subprocess handles:

    class Joiner(Thread):
        def __init__(self, q):
            self.__q = q
        def run(self):
            while True:
                child = self.__q.get()
                if child == None:
                    return
                child.join()
    

    Then, instead of p.join(), do joinq.put(p) and do a joinq.put(None) to signal the thread to stop. Make sure you use a FIFO queue.

提交回复
热议问题