Deliberately make an orphan process in python

前端 未结 1 565
醉酒成梦
醉酒成梦 2020-12-20 00:04

I have a python script (unix-like, based on RHEL), called MyScript, that has two functions, called A and B. I\'d like them to run in different, independent processes (detach

相关标签:
1条回答
  • 2020-12-20 00:46

    Here is just a random version of your original code that moves the functionality into a single call spawn_detached(callable). It keeps the detached process running even after the program exits:

    import time
    import os
    from multiprocessing import Process, current_process
    
    def spawn_detached(callable):
        p = _spawn_detached(0, callable)
        # give the process a moment to set up
        # and then kill the first child to detach
        # the second.
        time.sleep(.001)
        p.terminate()
    
    def _spawn_detached(count, callable):
        count += 1
        p = current_process()
        print 'Process #%d: %s (%d)' % (count, p.name, p.pid)
    
        if count < 2:
            name = 'child'
        elif count == 2:
            name = callable.func_name
        else:
            # we should now be inside of our detached process
            # so just call the function
            return callable()
    
        # otherwise, spawn another process, passing the counter as well
        p = Process(name=name, target=_spawn_detached, args=(count, callable)) 
        p.daemon = False 
        p.start()
        return p
    
    def operation():
        """ Just some arbitrary function """
        print "Entered detached process"
        time.sleep(15)
        print "Exiting detached process"
    
    
    if __name__ == "__main__":
        print 'starting main', os.getpid()
        p = spawn_detached(operation)
        print 'exiting main', os.getpid()
    
    0 讨论(0)
提交回复
热议问题