• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""A cache for FileWriters."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import threading
22
23from tensorflow.python.framework import ops
24from tensorflow.python.summary.writer.writer import FileWriter
25from tensorflow.python.util.tf_export import tf_export
26
27
28@tf_export(v1=['summary.FileWriterCache'])
29class FileWriterCache(object):
30  """Cache for file writers.
31
32  This class caches file writers, one per directory.
33  """
34  # Cache, keyed by directory.
35  _cache = {}
36
37  # Lock protecting _FILE_WRITERS.
38  _lock = threading.RLock()
39
40  @staticmethod
41  def clear():
42    """Clear cached summary writers. Currently only used for unit tests."""
43    with FileWriterCache._lock:
44      # Make sure all the writers are closed now (otherwise open file handles
45      # may hang around, blocking deletions on Windows).
46      for item in FileWriterCache._cache.values():
47        item.close()
48      FileWriterCache._cache = {}
49
50  @staticmethod
51  def get(logdir):
52    """Returns the FileWriter for the specified directory.
53
54    Args:
55      logdir: str, name of the directory.
56
57    Returns:
58      A `FileWriter`.
59    """
60    with FileWriterCache._lock:
61      if logdir not in FileWriterCache._cache:
62        FileWriterCache._cache[logdir] = FileWriter(
63            logdir, graph=ops.get_default_graph())
64      return FileWriterCache._cache[logdir]
65