Python: How to determine subprocess children have all finished running

后端 未结 4 1123
难免孤独
难免孤独 2021-02-10 07:14

I am trying to detect when an installation program finishes executing from within a Python script. Specifically, the application is the Oracle 10gR2 Database. Currently I am usi

4条回答
  •  余生分开走
    2021-02-10 07:54

    Everywhere I look seems to say it's not possible to solve this in the general case. I've whipped up a library called 'pidmon' that combines some answers for Windows and Linux and might do what you need.

    I'm planning to clean this up and put it on github, possibly called 'pidmon' or something like that. I'll post a link if/when I get it up.

    EDIT: It's available at http://github.com/dbarnett/python-pidmon.

    I made a special waitpid function that accepts a graft_func argument so that you can loosely define what sort of processes you want to wait for when they're not direct children:

    import pidmon
    pidmon.waitpid(oracle_subprocess.pid, recursive=True,
            graft_func=(lambda p: p.name == '???' and p.parent.pid == ???))
    

    or, as a shotgun approach, to just wait for any processes started since the call to waitpid to stop again, do:

    import pidmon
    pidmon.waitpid(oracle_subprocess.pid, graft_func=(lambda p: True))
    

    Note that this is still barely tested on Windows and seems very slow on Windows (but did I mention it's on github where it's easy to fork?). This should at least get you started, and if it works at all for you, I have plenty of ideas on how to optimize it.

提交回复
热议问题