import functools
import re
import threading
+import warnings
import numpy as np
```
"""
+ _count_lock = threading.Lock()
+ _active_session_count = 0 # GUARDED_BY(_count_lock)
+
def __init__(self, target='', graph=None, config=None):
"""Creates a new interactive TensorFlow session.
config.graph_options.place_pruned_graph = True
super(InteractiveSession, self).__init__(target, graph, config)
+ with InteractiveSession._count_lock:
+ if InteractiveSession._active_session_count > 0:
+ warnings.warn('An interactive session is already active. This can '
+ 'cause out-of-memory errors in some cases. You must '
+ 'explicitly call `InteractiveSession.close()` to release '
+ 'resources held by the other session(s).')
+ InteractiveSession._active_session_count += 1
+ self._closed = False
+
self._default_session = self.as_default()
self._default_session.enforce_nesting = False
self._default_session.__enter__()
def close(self):
"""Closes an `InteractiveSession`."""
super(InteractiveSession, self).close()
+ with InteractiveSession._count_lock:
+ if not self._closed:
+ InteractiveSession._active_session_count -= 1
+ self._closed = True
+ else:
+ return
if self._explicit_graph is not None:
self._default_graph.__exit__(None, None, None)
self._default_session.__exit__(None, None, None)
import sys
import threading
import time
+import warnings
import numpy as np
import six
# @test_util.with_c_api
class SessionTest(test_util.TensorFlowTestCase):
+ def setUp(self):
+ super(SessionTest, self).setUp()
+ warnings.simplefilter('always')
+
def testUseExistingGraph(self):
with ops.Graph().as_default() as g, ops.device('/cpu:0'):
a = constant_op.constant(6.0, shape=[1, 1])
self.assertAllEqual([[24.0]], e.eval())
sess.close()
+ def testMultipleInteractiveSessionsWarning(self):
+ # Reinitialize the global state to ensure that the expected warnings will
+ # be emitted.
+ session.InteractiveSession._active_session_count = 0 # pylint: disable=protected-access
+
+ sess = session.InteractiveSession()
+ sess.close()
+ # Opening and closing interactive sessions serially should not warn.
+ with warnings.catch_warnings(record=True) as w:
+ sess = session.InteractiveSession()
+ sess.close()
+ self.assertEqual(0, len(w))
+
+ with warnings.catch_warnings(record=True) as w:
+ sess = session.InteractiveSession()
+ self.assertEqual(0, len(w))
+ with warnings.catch_warnings(record=True) as w:
+ sess2 = session.InteractiveSession()
+ self.assertEqual(1, len(w))
+ self.assertTrue('An interactive session is already active. This can cause '
+ 'out-of-memory errors in some cases. You must explicitly '
+ 'call `InteractiveSession.close()` to release resources '
+ 'held by the other session(s).' in str(w[0].message))
+ sess2.close()
+ sess.close()
+
def testInteractivePlacePrunedGraph(self):
sess = session.InteractiveSession()