Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. """Example of launching distributed service and then bringint it down."""
  2.  
  3. import subprocess
  4. import tensorflow as tf
  5. import time
  6. import sys
  7.  
  8. flags = tf.flags
  9. flags.DEFINE_string("port1", "12222", "port of worker1")
  10. flags.DEFINE_string("port2", "12223", "port of worker2")
  11. flags.DEFINE_string("task", "", "internal use")
  12. FLAGS = flags.FLAGS
  13.  
  14. # setup local cluster from flags
  15. host = "127.0.0.1:"
  16. cluster = {"worker": [host+FLAGS.port1, host+FLAGS.port2]}
  17. clusterspec = tf.train.ClusterSpec(cluster).as_cluster_def()
  18.  
  19. if __name__=='__main__':
  20. if not FLAGS.task: # start servers and run client
  21.  
  22. # launch distributed service
  23. def runcmd(cmd): subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT)
  24. runcmd("python %s --task=0"%(sys.argv[0]))
  25. runcmd("python %s --task=1"%(sys.argv[0]))
  26. time.sleep(1)
  27.  
  28. # bring down distributed service
  29. sess = tf.Session("grpc://"+host+FLAGS.port1)
  30. queue0 = tf.FIFOQueue(1, tf.int32, shared_name="queue0")
  31. queue1 = tf.FIFOQueue(1, tf.int32, shared_name="queue1")
  32. with tf.device("/job:worker/task:0"):
  33. add_op0 = tf.add(tf.ones(()), tf.ones(()))
  34. with tf.device("/job:worker/task:1"):
  35. add_op1 = tf.add(tf.ones(()), tf.ones(()))
  36.  
  37. print("Running computation on server 0")
  38. print(sess.run(add_op0))
  39. print("Running computation on server 1")
  40. print(sess.run(add_op1))
  41.  
  42. print("Bringing down server 0")
  43. sess.run(queue0.enqueue(1))
  44. print("Bringing down server 1")
  45. sess.run(queue1.enqueue(1))
  46.  
  47. else: # Launch TensorFlow server
  48. server = tf.train.Server(clusterspec, config=None,
  49. job_name="worker",
  50. task_index=int(FLAGS.task))
  51. print("Starting server "+FLAGS.task)
  52. sess = tf.Session(server.target)
  53. queue = tf.FIFOQueue(1, tf.int32, shared_name="queue"+FLAGS.task)
  54. sess.run(queue.dequeue())
  55. print("Terminating server"+FLAGS.task)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement