Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(95931)

Side by Side Diff: Lib/asyncio/base_events.py

Issue 21998: asyncio: a new self-pipe should be created in the child process after fork
Patch Set: Created 4 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/asyncio/proactor_events.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Base implementation of event loop. 1 """Base implementation of event loop.
2 2
3 The event loop can be broken up into a multiplexer (the part 3 The event loop can be broken up into a multiplexer (the part
4 responsible for notifying us of I/O events) and the event loop proper, 4 responsible for notifying us of I/O events) and the event loop proper,
5 which wraps a multiplexer with functionality for scheduling callbacks, 5 which wraps a multiplexer with functionality for scheduling callbacks,
6 immediately or at a given time in the future. 6 immediately or at a given time in the future.
7 7
8 Whenever a public API takes a callback, subsequent positional 8 Whenever a public API takes a callback, subsequent positional
9 arguments will be passed to the callback if/when it is called. This 9 arguments will be passed to the callback if/when it is called. This
10 avoids the proliferation of trivial lambdas implementing closures. 10 avoids the proliferation of trivial lambdas implementing closures.
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 def __init__(self): 182 def __init__(self):
183 self._timer_cancelled_count = 0 183 self._timer_cancelled_count = 0
184 self._closed = False 184 self._closed = False
185 self._ready = collections.deque() 185 self._ready = collections.deque()
186 self._scheduled = [] 186 self._scheduled = []
187 self._default_executor = None 187 self._default_executor = None
188 self._internal_fds = 0 188 self._internal_fds = 0
189 # Identifier of the thread running the event loop, or None if the 189 # Identifier of the thread running the event loop, or None if the
190 # event loop is not running 190 # event loop is not running
191 self._thread_id = None 191 self._thread_id = None
192 self._pid = os.getpid()
192 self._clock_resolution = time.get_clock_info('monotonic').resolution 193 self._clock_resolution = time.get_clock_info('monotonic').resolution
193 self._exception_handler = None 194 self._exception_handler = None
194 self._debug = (not sys.flags.ignore_environment 195 self._debug = (not sys.flags.ignore_environment
195 and bool(os.environ.get('PYTHONASYNCIODEBUG'))) 196 and bool(os.environ.get('PYTHONASYNCIODEBUG')))
196 # In debug mode, if the execution of a callback or a step of a task 197 # In debug mode, if the execution of a callback or a step of a task
197 # exceed this duration in seconds, the slow callback/task is logged. 198 # exceed this duration in seconds, the slow callback/task is logged.
198 self.slow_callback_duration = 0.1 199 self.slow_callback_duration = 0.1
199 self._current_handle = None 200 self._current_handle = None
200 201
201 def __repr__(self): 202 def __repr__(self):
202 return ('<%s running=%s closed=%s debug=%s>' 203 return ('<%s running=%s closed=%s debug=%s>'
203 % (self.__class__.__name__, self.is_running(), 204 % (self.__class__.__name__, self.is_running(),
204 self.is_closed(), self.get_debug())) 205 self.is_closed(), self.get_debug()))
206
207 def _detect_fork(self):
208 pid = os.getpid()
209 if pid != self._pid:
210 self._pid = pid
211 self._at_fork()
212
213 def _at_fork(self):
214 pass
205 215
206 def create_task(self, coro): 216 def create_task(self, coro):
207 """Schedule a coroutine object. 217 """Schedule a coroutine object.
208 218
209 Return a task object. 219 Return a task object.
210 """ 220 """
211 self._check_closed() 221 self._check_closed()
212 task = tasks.Task(coro, loop=self) 222 task = tasks.Task(coro, loop=self)
213 if task._source_traceback: 223 if task._source_traceback:
214 del task._source_traceback[-1] 224 del task._source_traceback[-1]
(...skipping 889 matching lines...) Expand 10 before | Expand all | Expand 10 after
1104 handle = heapq.heappop(self._scheduled) 1114 handle = heapq.heappop(self._scheduled)
1105 handle._scheduled = False 1115 handle._scheduled = False
1106 1116
1107 timeout = None 1117 timeout = None
1108 if self._ready: 1118 if self._ready:
1109 timeout = 0 1119 timeout = 0
1110 elif self._scheduled: 1120 elif self._scheduled:
1111 # Compute the desired timeout. 1121 # Compute the desired timeout.
1112 when = self._scheduled[0]._when 1122 when = self._scheduled[0]._when
1113 timeout = max(0, when - self.time()) 1123 timeout = max(0, when - self.time())
1124
1125 # detect fork before using the selector
1126 self._detect_fork()
1114 1127
1115 if self._debug and timeout != 0: 1128 if self._debug and timeout != 0:
1116 t0 = self.time() 1129 t0 = self.time()
1117 event_list = self._selector.select(timeout) 1130 event_list = self._selector.select(timeout)
1118 dt = self.time() - t0 1131 dt = self.time() - t0
1119 if dt >= 1.0: 1132 if dt >= 1.0:
1120 level = logging.INFO 1133 level = logging.INFO
1121 else: 1134 else:
1122 level = logging.DEBUG 1135 level = logging.DEBUG
1123 nevent = len(event_list) 1136 nevent = len(event_list)
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1170 self._current_handle = None 1183 self._current_handle = None
1171 else: 1184 else:
1172 handle._run() 1185 handle._run()
1173 handle = None # Needed to break cycles when an exception occurs. 1186 handle = None # Needed to break cycles when an exception occurs.
1174 1187
1175 def get_debug(self): 1188 def get_debug(self):
1176 return self._debug 1189 return self._debug
1177 1190
1178 def set_debug(self, enabled): 1191 def set_debug(self, enabled):
1179 self._debug = enabled 1192 self._debug = enabled
OLDNEW
« no previous file with comments | « no previous file | Lib/asyncio/proactor_events.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+