Caffe2 - Python API
A deep learning, cross platform ML framework
__init__.py
1 r"""Utility classes & functions for data loading. Code in this folder is mostly
2 used by ../dataloder.py.
3 
4 A lot of multiprocessing is used in data loading, which only supports running
5 functions defined in global environment (py2 can't serialize static methods).
6 Therefore, for code tidiness we put these functions into different files in this
7 folder.
8 """
9 
10 import sys
11 import traceback
12 import atexit
13 
14 
15 IS_WINDOWS = sys.platform == "win32"
16 
17 
18 # NOTE [ Python Traceback Reference Cycle Problem ]
19 #
20 # When using sys.exc_info(), it is important to **not** store the exc_info[2],
21 # which is the traceback, because otherwise you will run into the traceback
22 # reference cycle problem, i.e., the traceback holding reference to the frame,
23 # and the frame (which holds reference to all the object in its temporary scope)
24 # holding reference the traceback.
25 
26 
27 class ExceptionWrapper(object):
28  r"""Wraps an exception plus traceback to communicate across threads"""
29  def __init__(self, exc_info):
30  # It is important that we don't store exc_info, see
31  # NOTE [ Python Traceback Reference Cycle Problem ]
32  self.exc_type = exc_info[0]
33  self.exc_msg = "".join(traceback.format_exception(*exc_info))
34 
35 
36 MP_STATUS_CHECK_INTERVAL = 5.0
37 r"""Interval (in seconds) to check status of processes to avoid hanging in
38  multiprocessing data loading. This is mainly used in getting data from
39  another process, in which case we need to periodically check whether the
40  sender is alive to prevent hanging."""
41 
42 
43 python_exit_status = False
44 r"""Whether Python is shutting down. This flag is guaranteed to be set before
45 the Python core library resources are freed, but Python may already be exiting
46 for some time when this is set.
47 
48 Hook to set this flag is `_set_python_exit_flag`, and is inspired by a similar
49 hook in Python 3.7 multiprocessing library:
50 https://github.com/python/cpython/blob/d4d60134b29290049e28df54f23493de4f1824b6/Lib/multiprocessing/util.py#L277-L327
51 """
52 
53 
54 def _set_python_exit_flag():
55  global python_exit_status
56  python_exit_status = True
57 
58 atexit.register(_set_python_exit_flag)
59 
60 
61 from . import worker, signal_handling, pin_memory, collate