Using subprocess with select and pty hangs when capturing output

前端 未结 4 1243
说谎
说谎 2021-01-31 12:41

I\'m trying to write a python program that is able to interact with other programs. That means sending stdin and receiving stdout data. I cannot use pexpect (although it definit

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-31 13:20

    When your child process exits - your parent process gets SIGCHLD signal. By default this signal is ignored but you can intercept it:

    import sys
    import signal
    
    def handler(signum, frame):
        print 'Child has exited!'
        sys.exit(0)
    
    signal.signal(signal.SIGCHLD, handler)
    

    The signal should also break the blocking syscall to 'select' or 'read' (or whatever you are in) and let you do whatever you have to (cleanup, exit, etc.) in handler function.

提交回复
热议问题