Advertisement
homer512

sleep repeat

May 25th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #!/usr/bin/python2
  2.  
  3.  
  4. import multiprocessing
  5. import sys
  6. import time
  7. import os
  8.  
  9.  
  10. def interact(pipe, stdin):
  11.     readfd, writefd = pipe
  12.     readfd.close()
  13.     print "Enter seconds to wait"
  14.     for line in stdin:
  15.         seconds = float(line)
  16.         print "Requesting additional %g seconds. Enter more if you like" \
  17.             % seconds
  18.         writefd.send(seconds)
  19.  
  20.  
  21. def wait(pipe, interact_process):
  22.     readfd, writefd = pipe
  23.     writefd.close()
  24.     time.sleep(4)
  25.     while readfd.poll():
  26.         seconds = readfd.recv()
  27.         print "Waiting additional %g seconds" % seconds
  28.         time.sleep(seconds)
  29.     print "Killing child"
  30.     interact_process.terminate()
  31.  
  32.  
  33. def main():
  34.     # use simplex connection between both processes to send additional
  35.     # waiting time
  36.     pipe = multiprocessing.Pipe(False)
  37.     # duplicate stdin because multiprocessing will close the original fd
  38.     # in the child
  39.     stdinfd = os.dup(sys.stdin.fileno())
  40.     stdin = os.fdopen(stdinfd, 'rt', 1)
  41.     child = multiprocessing.Process(target=interact, args=(pipe, stdin))
  42.     child.start()
  43.     wait(pipe, child)
  44.     child.join()
  45.  
  46.  
  47. if __name__ == '__main__':
  48.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement