#!/usr/bin/python2 import multiprocessing import sys import time import os def interact(pipe, stdin): readfd, writefd = pipe readfd.close() print "Enter seconds to wait" for line in stdin: seconds = float(line) print "Requesting additional %g seconds. Enter more if you like" \ % seconds writefd.send(seconds) def wait(pipe, interact_process): readfd, writefd = pipe writefd.close() time.sleep(4) while readfd.poll(): seconds = readfd.recv() print "Waiting additional %g seconds" % seconds time.sleep(seconds) print "Killing child" interact_process.terminate() def main(): # use simplex connection between both processes to send additional # waiting time pipe = multiprocessing.Pipe(False) # duplicate stdin because multiprocessing will close the original fd # in the child stdinfd = os.dup(sys.stdin.fileno()) stdin = os.fdopen(stdinfd, 'rt', 1) child = multiprocessing.Process(target=interact, args=(pipe, stdin)) child.start() wait(pipe, child) child.join() if __name__ == '__main__': main()
Comments