Jia Li
Jia Li

Reputation: 504

Tensorflow run session before multiprocess

I am using tensorflow and python multi processing in one of my project. I have noticed that if I initialize a session before the multi processing, the multi processing seems to stuck somewhere.

My code looks something like this:

import tensorflow as tf
from multiprocessing.pool import Pool
graph = tf.Graph()
with graph.as_default():
    X = tf.Variable([10, 1])
    init = tf.initialize_all_variables()
graph.finalize()

def run(i):
    sess = tf.Session(graph=graph)
    sess.run(init)
    print sess.run(X)
#uncomment for the bug
#sess = tf.Session(graph=graph)
#sess.close()
p = Pool(4)
res = p.map(run, [1,2,3])

And the message I got when interrupting:

Process PoolWorker-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 102, in worker
    task = get()
  File "/usr/lib/python2.7/multiprocessing/queues.py", line 374, in get
    racquire()
KeyboardInterrupt

Upvotes: 0

Views: 683

Answers (1)

Gregory Begelman
Gregory Begelman

Reputation: 554

What are you trying to achieve? Why do you try to create several sessions for the same graph and run them in parallel? As @fabrizioM mentioned, TF takes care of distributing computations between CPU and GPU, if properly configured. So the way you try to use TF is not really supported.

Upvotes: 2

Related Questions