blob: b7cc35122d4d70733f5f885475519895388cec66 [file] [log] [blame]
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07001"""Event loop and event loop policy."""
2
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -08003__all__ = ['AbstractEventLoopPolicy',
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07004 'AbstractEventLoop', 'AbstractServer',
5 'Handle', 'TimerHandle',
6 'get_event_loop_policy', 'set_event_loop_policy',
7 'get_event_loop', 'set_event_loop', 'new_event_loop',
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -08008 'get_child_watcher', 'set_child_watcher',
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07009 ]
10
Victor Stinner307bccc2014-06-12 18:39:26 +020011import functools
12import inspect
Victor Stinner313a9802014-07-29 12:58:23 +020013import reprlib
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070014import socket
Victor Stinner313a9802014-07-29 12:58:23 +020015import subprocess
Victor Stinner307bccc2014-06-12 18:39:26 +020016import sys
Victor Stinner313a9802014-07-29 12:58:23 +020017import threading
18import traceback
Victor Stinner307bccc2014-06-12 18:39:26 +020019
20
21_PY34 = sys.version_info >= (3, 4)
22
Victor Stinner975735f2014-06-25 21:41:58 +020023
Victor Stinner307bccc2014-06-12 18:39:26 +020024def _get_function_source(func):
25 if _PY34:
26 func = inspect.unwrap(func)
27 elif hasattr(func, '__wrapped__'):
28 func = func.__wrapped__
29 if inspect.isfunction(func):
30 code = func.__code__
31 return (code.co_filename, code.co_firstlineno)
32 if isinstance(func, functools.partial):
33 return _get_function_source(func.func)
34 if _PY34 and isinstance(func, functools.partialmethod):
35 return _get_function_source(func.func)
36 return None
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070037
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070038
Victor Stinner975735f2014-06-25 21:41:58 +020039def _format_args(args):
Victor Stinner313a9802014-07-29 12:58:23 +020040 """Format function arguments.
41
42 Special case for a single parameter: ('hello',) is formatted as ('hello').
43 """
44 # use reprlib to limit the length of the output
45 args_repr = reprlib.repr(args)
Victor Stinner975735f2014-06-25 21:41:58 +020046 if len(args) == 1 and args_repr.endswith(',)'):
47 args_repr = args_repr[:-2] + ')'
48 return args_repr
49
50
51def _format_callback(func, args, suffix=''):
52 if isinstance(func, functools.partial):
53 if args is not None:
54 suffix = _format_args(args) + suffix
55 return _format_callback(func.func, func.args, suffix)
56
57 func_repr = getattr(func, '__qualname__', None)
58 if not func_repr:
59 func_repr = repr(func)
60
61 if args is not None:
62 func_repr += _format_args(args)
63 if suffix:
64 func_repr += suffix
65
66 source = _get_function_source(func)
67 if source:
68 func_repr += ' at %s:%s' % source
69 return func_repr
70
71
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070072class Handle:
73 """Object returned by callback registration methods."""
74
Victor Stinner80f53aa2014-06-27 13:52:20 +020075 __slots__ = ('_callback', '_args', '_cancelled', '_loop',
Victor Stinner1b38bc62014-09-17 23:24:13 +020076 '_source_traceback', '_repr', '__weakref__')
Yury Selivanovb1317782014-02-12 17:01:52 -050077
Yury Selivanov569efa22014-02-18 18:02:19 -050078 def __init__(self, callback, args, loop):
Victor Stinnerdc62b7e2014-02-10 00:45:44 +010079 assert not isinstance(callback, Handle), 'A Handle is not a callback'
Yury Selivanov569efa22014-02-18 18:02:19 -050080 self._loop = loop
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070081 self._callback = callback
82 self._args = args
83 self._cancelled = False
Victor Stinner1b38bc62014-09-17 23:24:13 +020084 self._repr = None
Victor Stinner80f53aa2014-06-27 13:52:20 +020085 if self._loop.get_debug():
86 self._source_traceback = traceback.extract_stack(sys._getframe(1))
87 else:
88 self._source_traceback = None
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070089
Victor Stinner1b38bc62014-09-17 23:24:13 +020090 def _repr_info(self):
Victor Stinnerf68bd882014-07-10 22:32:58 +020091 info = [self.__class__.__name__]
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070092 if self._cancelled:
Victor Stinner975735f2014-06-25 21:41:58 +020093 info.append('cancelled')
Victor Stinnerf68bd882014-07-10 22:32:58 +020094 if self._callback is not None:
95 info.append(_format_callback(self._callback, self._args))
96 if self._source_traceback:
97 frame = self._source_traceback[-1]
98 info.append('created at %s:%s' % (frame[0], frame[1]))
Victor Stinner1b38bc62014-09-17 23:24:13 +020099 return info
100
101 def __repr__(self):
102 if self._repr is not None:
103 return self._repr
104 info = self._repr_info()
Victor Stinnerf68bd882014-07-10 22:32:58 +0200105 return '<%s>' % ' '.join(info)
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700106
107 def cancel(self):
108 self._cancelled = True
Victor Stinner1b38bc62014-09-17 23:24:13 +0200109 if self._loop.get_debug():
110 # Keep a representation in debug mode to keep callback and
111 # parameters. For example, to log the warning "Executing <Handle
112 # ...> took 2.5 second"
113 self._repr = repr(self)
Victor Stinnerf68bd882014-07-10 22:32:58 +0200114 self._callback = None
115 self._args = None
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700116
117 def _run(self):
118 try:
119 self._callback(*self._args)
Yury Selivanov569efa22014-02-18 18:02:19 -0500120 except Exception as exc:
Victor Stinner17b53f12014-06-26 01:35:45 +0200121 cb = _format_callback(self._callback, self._args)
122 msg = 'Exception in callback {}'.format(cb)
Victor Stinner80f53aa2014-06-27 13:52:20 +0200123 context = {
Yury Selivanov569efa22014-02-18 18:02:19 -0500124 'message': msg,
125 'exception': exc,
126 'handle': self,
Victor Stinner80f53aa2014-06-27 13:52:20 +0200127 }
128 if self._source_traceback:
129 context['source_traceback'] = self._source_traceback
130 self._loop.call_exception_handler(context)
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700131 self = None # Needed to break cycles when an exception occurs.
132
133
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700134class TimerHandle(Handle):
135 """Object returned by timed callback registration methods."""
136
Yury Selivanovb1317782014-02-12 17:01:52 -0500137 __slots__ = ['_when']
138
Yury Selivanov569efa22014-02-18 18:02:19 -0500139 def __init__(self, when, callback, args, loop):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700140 assert when is not None
Yury Selivanov569efa22014-02-18 18:02:19 -0500141 super().__init__(callback, args, loop)
Victor Stinner80f53aa2014-06-27 13:52:20 +0200142 if self._source_traceback:
143 del self._source_traceback[-1]
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700144 self._when = when
145
Victor Stinner1b38bc62014-09-17 23:24:13 +0200146 def _repr_info(self):
147 info = super()._repr_info()
148 pos = 2 if self._cancelled else 1
149 info.insert(pos, 'when=%s' % self._when)
150 return info
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700151
152 def __hash__(self):
153 return hash(self._when)
154
155 def __lt__(self, other):
156 return self._when < other._when
157
158 def __le__(self, other):
159 if self._when < other._when:
160 return True
161 return self.__eq__(other)
162
163 def __gt__(self, other):
164 return self._when > other._when
165
166 def __ge__(self, other):
167 if self._when > other._when:
168 return True
169 return self.__eq__(other)
170
171 def __eq__(self, other):
172 if isinstance(other, TimerHandle):
173 return (self._when == other._when and
174 self._callback == other._callback and
175 self._args == other._args and
176 self._cancelled == other._cancelled)
177 return NotImplemented
178
179 def __ne__(self, other):
180 equal = self.__eq__(other)
181 return NotImplemented if equal is NotImplemented else not equal
182
183
184class AbstractServer:
Victor Stinnercf6f72e2013-12-03 18:23:52 +0100185 """Abstract server returned by create_server()."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700186
187 def close(self):
188 """Stop serving. This leaves existing connections open."""
189 return NotImplemented
190
191 def wait_closed(self):
192 """Coroutine to wait until service is closed."""
193 return NotImplemented
194
195
196class AbstractEventLoop:
197 """Abstract event loop."""
198
199 # Running and stopping the event loop.
200
201 def run_forever(self):
202 """Run the event loop until stop() is called."""
203 raise NotImplementedError
204
205 def run_until_complete(self, future):
206 """Run the event loop until a Future is done.
207
208 Return the Future's result, or raise its exception.
209 """
210 raise NotImplementedError
211
212 def stop(self):
213 """Stop the event loop as soon as reasonable.
214
215 Exactly how soon that is may depend on the implementation, but
216 no more I/O callbacks should be scheduled.
217 """
218 raise NotImplementedError
219
220 def is_running(self):
221 """Return whether the event loop is currently running."""
222 raise NotImplementedError
223
Victor Stinner896a25a2014-07-08 11:29:25 +0200224 def is_closed(self):
225 """Returns True if the event loop was closed."""
226 raise NotImplementedError
227
Guido van Rossume3f52ef2013-11-01 14:19:04 -0700228 def close(self):
229 """Close the loop.
230
231 The loop should not be running.
232
233 This is idempotent and irreversible.
234
235 No other methods should be called after this one.
236 """
237 raise NotImplementedError
238
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700239 # Methods scheduling callbacks. All these return Handles.
240
241 def call_soon(self, callback, *args):
242 return self.call_later(0, callback, *args)
243
244 def call_later(self, delay, callback, *args):
245 raise NotImplementedError
246
247 def call_at(self, when, callback, *args):
248 raise NotImplementedError
249
250 def time(self):
251 raise NotImplementedError
252
Victor Stinner896a25a2014-07-08 11:29:25 +0200253 # Method scheduling a coroutine object: create a task.
254
255 def create_task(self, coro):
256 raise NotImplementedError
257
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700258 # Methods for interacting with threads.
259
260 def call_soon_threadsafe(self, callback, *args):
261 raise NotImplementedError
262
263 def run_in_executor(self, executor, callback, *args):
264 raise NotImplementedError
265
266 def set_default_executor(self, executor):
267 raise NotImplementedError
268
269 # Network I/O methods returning Futures.
270
271 def getaddrinfo(self, host, port, *, family=0, type=0, proto=0, flags=0):
272 raise NotImplementedError
273
274 def getnameinfo(self, sockaddr, flags=0):
275 raise NotImplementedError
276
277 def create_connection(self, protocol_factory, host=None, port=None, *,
278 ssl=None, family=0, proto=0, flags=0, sock=None,
Guido van Rossum21c85a72013-11-01 14:16:54 -0700279 local_addr=None, server_hostname=None):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700280 raise NotImplementedError
281
282 def create_server(self, protocol_factory, host=None, port=None, *,
283 family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE,
284 sock=None, backlog=100, ssl=None, reuse_address=None):
285 """A coroutine which creates a TCP server bound to host and port.
286
287 The return value is a Server object which can be used to stop
288 the service.
289
290 If host is an empty string or None all interfaces are assumed
291 and a list of multiple sockets will be returned (most likely
292 one for IPv4 and another one for IPv6).
293
294 family can be set to either AF_INET or AF_INET6 to force the
295 socket to use IPv4 or IPv6. If not set it will be determined
296 from host (defaults to AF_UNSPEC).
297
298 flags is a bitmask for getaddrinfo().
299
300 sock can optionally be specified in order to use a preexisting
301 socket object.
302
303 backlog is the maximum number of queued connections passed to
304 listen() (defaults to 100).
305
306 ssl can be set to an SSLContext to enable SSL over the
307 accepted connections.
308
309 reuse_address tells the kernel to reuse a local socket in
310 TIME_WAIT state, without waiting for its natural timeout to
311 expire. If not specified will automatically be set to True on
312 UNIX.
313 """
314 raise NotImplementedError
315
Yury Selivanovb057c522014-02-18 12:15:06 -0500316 def create_unix_connection(self, protocol_factory, path, *,
317 ssl=None, sock=None,
318 server_hostname=None):
319 raise NotImplementedError
320
321 def create_unix_server(self, protocol_factory, path, *,
322 sock=None, backlog=100, ssl=None):
323 """A coroutine which creates a UNIX Domain Socket server.
324
Yury Selivanovdec1a452014-02-18 22:27:48 -0500325 The return value is a Server object, which can be used to stop
Yury Selivanovb057c522014-02-18 12:15:06 -0500326 the service.
327
328 path is a str, representing a file systsem path to bind the
329 server socket to.
330
331 sock can optionally be specified in order to use a preexisting
332 socket object.
333
334 backlog is the maximum number of queued connections passed to
335 listen() (defaults to 100).
336
337 ssl can be set to an SSLContext to enable SSL over the
338 accepted connections.
339 """
340 raise NotImplementedError
341
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700342 def create_datagram_endpoint(self, protocol_factory,
343 local_addr=None, remote_addr=None, *,
344 family=0, proto=0, flags=0):
345 raise NotImplementedError
346
Guido van Rossume3f52ef2013-11-01 14:19:04 -0700347 # Pipes and subprocesses.
348
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700349 def connect_read_pipe(self, protocol_factory, pipe):
Victor Stinnera5b257a2014-05-29 00:14:03 +0200350 """Register read pipe in event loop. Set the pipe to non-blocking mode.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700351
352 protocol_factory should instantiate object with Protocol interface.
Victor Stinnera5b257a2014-05-29 00:14:03 +0200353 pipe is a file-like object.
354 Return pair (transport, protocol), where transport supports the
Guido van Rossum9204af42013-11-30 15:35:42 -0800355 ReadTransport interface."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700356 # The reason to accept file-like object instead of just file descriptor
357 # is: we need to own pipe and close it at transport finishing
358 # Can got complicated errors if pass f.fileno(),
359 # close fd in pipe transport then close f and vise versa.
360 raise NotImplementedError
361
362 def connect_write_pipe(self, protocol_factory, pipe):
Yury Selivanovdec1a452014-02-18 22:27:48 -0500363 """Register write pipe in event loop.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700364
365 protocol_factory should instantiate object with BaseProtocol interface.
366 Pipe is file-like object already switched to nonblocking.
367 Return pair (transport, protocol), where transport support
Guido van Rossum9204af42013-11-30 15:35:42 -0800368 WriteTransport interface."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700369 # The reason to accept file-like object instead of just file descriptor
370 # is: we need to own pipe and close it at transport finishing
371 # Can got complicated errors if pass f.fileno(),
372 # close fd in pipe transport then close f and vise versa.
373 raise NotImplementedError
374
375 def subprocess_shell(self, protocol_factory, cmd, *, stdin=subprocess.PIPE,
376 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
377 **kwargs):
378 raise NotImplementedError
379
380 def subprocess_exec(self, protocol_factory, *args, stdin=subprocess.PIPE,
381 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
382 **kwargs):
383 raise NotImplementedError
384
385 # Ready-based callback registration methods.
386 # The add_*() methods return None.
387 # The remove_*() methods return True if something was removed,
388 # False if there was nothing to delete.
389
390 def add_reader(self, fd, callback, *args):
391 raise NotImplementedError
392
393 def remove_reader(self, fd):
394 raise NotImplementedError
395
396 def add_writer(self, fd, callback, *args):
397 raise NotImplementedError
398
399 def remove_writer(self, fd):
400 raise NotImplementedError
401
402 # Completion based I/O methods returning Futures.
403
404 def sock_recv(self, sock, nbytes):
405 raise NotImplementedError
406
407 def sock_sendall(self, sock, data):
408 raise NotImplementedError
409
410 def sock_connect(self, sock, address):
411 raise NotImplementedError
412
413 def sock_accept(self, sock):
414 raise NotImplementedError
415
416 # Signal handling.
417
418 def add_signal_handler(self, sig, callback, *args):
419 raise NotImplementedError
420
421 def remove_signal_handler(self, sig):
422 raise NotImplementedError
423
Yury Selivanov569efa22014-02-18 18:02:19 -0500424 # Error handlers.
425
426 def set_exception_handler(self, handler):
427 raise NotImplementedError
428
429 def default_exception_handler(self, context):
430 raise NotImplementedError
431
432 def call_exception_handler(self, context):
433 raise NotImplementedError
434
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100435 # Debug flag management.
436
437 def get_debug(self):
438 raise NotImplementedError
439
440 def set_debug(self, enabled):
441 raise NotImplementedError
442
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700443
444class AbstractEventLoopPolicy:
445 """Abstract policy for accessing the event loop."""
446
447 def get_event_loop(self):
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200448 """Get the event loop for the current context.
449
450 Returns an event loop object implementing the BaseEventLoop interface,
451 or raises an exception in case no event loop has been set for the
452 current context and the current policy does not specify to create one.
453
454 It should never return None."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700455 raise NotImplementedError
456
457 def set_event_loop(self, loop):
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200458 """Set the event loop for the current context to loop."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700459 raise NotImplementedError
460
461 def new_event_loop(self):
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200462 """Create and return a new event loop object according to this
463 policy's rules. If there's need to set this loop as the event loop for
464 the current context, set_event_loop must be called explicitly."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700465 raise NotImplementedError
466
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800467 # Child processes handling (Unix only).
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700468
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800469 def get_child_watcher(self):
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200470 "Get the watcher for child processes."
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800471 raise NotImplementedError
472
473 def set_child_watcher(self, watcher):
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200474 """Set the watcher for child processes."""
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800475 raise NotImplementedError
476
477
478class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700479 """Default policy implementation for accessing the event loop.
480
481 In this policy, each thread has its own event loop. However, we
482 only automatically create an event loop by default for the main
483 thread; other threads by default have no event loop.
484
485 Other policies may have different rules (e.g. a single global
486 event loop, or automatically creating an event loop per thread, or
487 using some other notion of context to which an event loop is
488 associated).
489 """
490
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800491 _loop_factory = None
492
493 class _Local(threading.local):
494 _loop = None
495 _set_called = False
496
497 def __init__(self):
498 self._local = self._Local()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700499
500 def get_event_loop(self):
501 """Get the event loop.
502
503 This may be None or an instance of EventLoop.
504 """
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800505 if (self._local._loop is None and
506 not self._local._set_called and
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700507 isinstance(threading.current_thread(), threading._MainThread)):
Guido van Rossumcced0762013-11-27 10:37:13 -0800508 self.set_event_loop(self.new_event_loop())
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800509 assert self._local._loop is not None, \
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700510 ('There is no current event loop in thread %r.' %
511 threading.current_thread().name)
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800512 return self._local._loop
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700513
514 def set_event_loop(self, loop):
515 """Set the event loop."""
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800516 self._local._set_called = True
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700517 assert loop is None or isinstance(loop, AbstractEventLoop)
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800518 self._local._loop = loop
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700519
520 def new_event_loop(self):
521 """Create a new event loop.
522
523 You must call set_event_loop() to make this the current event
524 loop.
525 """
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800526 return self._loop_factory()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700527
528
529# Event loop policy. The policy itself is always global, even if the
530# policy's rules say that there is an event loop per thread (or other
531# notion of context). The default policy is installed by the first
532# call to get_event_loop_policy().
533_event_loop_policy = None
534
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800535# Lock for protecting the on-the-fly creation of the event loop policy.
536_lock = threading.Lock()
537
538
539def _init_event_loop_policy():
540 global _event_loop_policy
541 with _lock:
542 if _event_loop_policy is None: # pragma: no branch
543 from . import DefaultEventLoopPolicy
544 _event_loop_policy = DefaultEventLoopPolicy()
545
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700546
547def get_event_loop_policy():
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200548 """Get the current event loop policy."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700549 if _event_loop_policy is None:
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800550 _init_event_loop_policy()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700551 return _event_loop_policy
552
553
554def set_event_loop_policy(policy):
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200555 """Set the current event loop policy.
556
557 If policy is None, the default policy is restored."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700558 global _event_loop_policy
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700559 assert policy is None or isinstance(policy, AbstractEventLoopPolicy)
560 _event_loop_policy = policy
561
562
563def get_event_loop():
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200564 """Equivalent to calling get_event_loop_policy().get_event_loop()."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700565 return get_event_loop_policy().get_event_loop()
566
567
568def set_event_loop(loop):
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200569 """Equivalent to calling get_event_loop_policy().set_event_loop(loop)."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700570 get_event_loop_policy().set_event_loop(loop)
571
572
573def new_event_loop():
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200574 """Equivalent to calling get_event_loop_policy().new_event_loop()."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700575 return get_event_loop_policy().new_event_loop()
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800576
577
578def get_child_watcher():
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200579 """Equivalent to calling get_event_loop_policy().get_child_watcher()."""
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800580 return get_event_loop_policy().get_child_watcher()
581
582
583def set_child_watcher(watcher):
Victor Stinnerf9e49dd2014-06-05 12:06:44 +0200584 """Equivalent to calling
585 get_event_loop_policy().set_child_watcher(watcher)."""
Guido van Rossum0eaa5ac2013-11-04 15:50:46 -0800586 return get_event_loop_policy().set_child_watcher(watcher)