Python join a process without blocking parent

后端 未结 5 1432
孤独总比滥情好
孤独总比滥情好 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:48

    If you don't care about when and whether the child terminates, and you just want to avoid the child ending up as a zombie process, then you can do a double-fork, so that the grandchild ends up being a child of init. In code:

    def child(*args):
      p = Process(target=grandchild, args=args)
      p.start()
      os._exit(0)
    
    def grandchild(filename, wDir, dDir):
      # Open filename and extract the url
      ...
      # Download the file and to the dDir directory
      ...
      # Delete filename from the watch directory
      ...
      # exit cleanly
      os._exit(0)
    

提交回复
热议问题