Is it possible to have multiple sessions in tensorflow? Is it possible to print number of sessions in tensorflow?
def test_print_number_of_sessions():
sess1 = tf.Session()
sess2 = tf.Session()
//print_number_of_sessions
Is it possible to have multiple sessions in tensorflow? Is it possible to print number of sessions in tensorflow?
def test_print_number_of_sessions():
sess1 = tf.Session()
sess2 = tf.Session()
//print_number_of_sessions
You can have multiple sessions per graph, but there is no direct way of getting all the open sessions in a graph. The internal C data structure for the graph does have a collection with all the existing sessions, but unfortunately the Python counter-part of that (the ._c_graph
attribute of tf.Graph
objects) is just an opaque pointer without type information.
One possible solution is to use your own session wrapper that tracks the open sessions per graph. This is one possible way to do that.
import tensorflow as tf
import collections
class TrackedSession(tf.Session):
_sessions = collections.defaultdict(list)
def __init__(self, target='', graph=None, config=None):
super(tf.Session, self).__init__(target=target, graph=graph, config=config)
TrackedSession._sessions[self.graph].append(self)
def close(self):
super(tf.Session, self).close()
TrackedSession._sessions[self.graph].remove(self)
@classmethod
def get_open_sessions(cls, g=None):
g = g or tf.get_default_graph()
return list(cls._sessions[g])
print(TrackedSession.get_open_sessions())
# []
sess1 = TrackedSession()
print(TrackedSession.get_open_sessions())
# [<__main__.TrackedSession object at 0x000001D75B0C77F0>]
sess2 = TrackedSession()
print(TrackedSession.get_open_sessions())
# [<__main__.TrackedSession object at 0x000001D75B0C77F0>, <__main__.TrackedSession object at 0x000001D75B0C7A58>]
sess1.close()
print(TrackedSession.get_open_sessions())
# [<__main__.TrackedSession object at 0x000001D75B0C7A58>]
sess2.close()
print(TrackedSession.get_open_sessions())
# []
This however restricts you to use this custom session type, which may not be good enough depending on the scenario (e.g. if the session is opened by some external code, for example when you use Keras).
This could be helpful:
tf.InteractiveSession._active_session_count