Coverage for Lib/asyncio/base_events.py: 89%
1188 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 05:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 05:46 +0000
1"""Base implementation of event loop.
3The event loop can be broken up into a multiplexer (the part
4responsible for notifying us of I/O events) and the event loop proper,
5which wraps a multiplexer with functionality for scheduling callbacks,
6immediately or at a given time in the future.
8Whenever a public API takes a callback, subsequent positional
9arguments will be passed to the callback if/when it is called. This
10avoids the proliferation of trivial lambdas implementing closures.
11Keyword arguments for the callback are not supported; this is a
12conscious design decision, leaving the door open for keyword arguments
13to modify the meaning of the API call itself.
14"""
16import collections
17import contextvars
18import collections.abc
19import concurrent.futures
20import errno
21import heapq
22import itertools
23import math
24import os
25import socket
26import stat
27import subprocess
28import sys
29import threading
30import time
31import traceback
32import warnings
33import weakref
34import inspect
36try:
37 import ssl
38except ImportError: # pragma: no cover
39 ssl = None
41from . import constants
42from . import coroutines
43from . import events
44from . import exceptions
45from . import futures
46from . import protocols
47from . import sslproto
48from . import staggered
49from . import tasks
50from . import timeouts
51from . import transports
52from . import trsock
53from .log import logger
56__all__ = 'BaseEventLoop','Server',
59# Minimum number of _scheduled timer handles before cleanup of
60# cancelled handles is performed.
61_MIN_SCHEDULED_TIMER_HANDLES = 100
63# Minimum fraction of _scheduled timer handles that are cancelled
64# before cleanup of cancelled handles is performed.
65_MIN_CANCELLED_TIMER_HANDLES_FRACTION = 0.5
68_HAS_IPv6 = hasattr(socket, 'AF_INET6')
70# Maximum timeout passed to select to avoid OS limitations
71MAXIMUM_SELECT_TIMEOUT = 24 * 3600
74def _format_handle(handle):
75 cb = handle._callback
76 if isinstance(getattr(cb, '__self__', None), tasks.Task):
77 # format the task
78 return repr(cb.__self__)
79 else:
80 return str(handle)
83def _format_pipe(fd):
84 if fd == subprocess.PIPE:
85 return '<pipe>'
86 elif fd == subprocess.STDOUT:
87 return '<stdout>'
88 else:
89 return repr(fd)
92def _set_reuseport(sock):
93 if not hasattr(socket, 'SO_REUSEPORT'):
94 raise ValueError('reuse_port not supported by socket module')
95 else:
96 try:
97 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
98 except OSError:
99 raise ValueError('reuse_port not supported by socket module, '
100 'SO_REUSEPORT defined but not implemented.')
103def _ipaddr_info(host, port, family, type, proto, flowinfo=0, scopeid=0):
104 # Try to skip getaddrinfo if "host" is already an IP. Users might have
105 # handled name resolution in their own code and pass in resolved IPs.
106 if not hasattr(socket, 'inet_pton'):
107 return
109 if proto not in {0, socket.IPPROTO_TCP, socket.IPPROTO_UDP} or \
110 host is None:
111 return None
113 if type == socket.SOCK_STREAM:
114 proto = socket.IPPROTO_TCP
115 elif type == socket.SOCK_DGRAM:
116 proto = socket.IPPROTO_UDP
117 else:
118 return None
120 if port is None:
121 port = 0
122 elif isinstance(port, bytes) and port == b'':
123 port = 0
124 elif isinstance(port, str) and port == '':
125 port = 0
126 else:
127 # If port's a service name like "http", don't skip getaddrinfo.
128 try:
129 port = int(port)
130 except (TypeError, ValueError):
131 return None
133 if family == socket.AF_UNSPEC:
134 afs = [socket.AF_INET]
135 if _HAS_IPv6: 135 ↛ 140line 135 didn't jump to line 140 because the condition on line 135 was always true
136 afs.append(socket.AF_INET6)
137 else:
138 afs = [family]
140 if isinstance(host, bytes):
141 host = host.decode('idna')
142 if '%' in host:
143 # Linux's inet_pton doesn't accept an IPv6 zone index after host,
144 # like '::1%lo0'.
145 return None
147 for af in afs:
148 try:
149 socket.inet_pton(af, host)
150 # The host has already been resolved.
151 if _HAS_IPv6 and af == socket.AF_INET6:
152 return af, type, proto, '', (host, port, flowinfo, scopeid)
153 else:
154 return af, type, proto, '', (host, port)
155 except OSError:
156 pass
158 # "host" is not an IP address.
159 return None
162def _interleave_addrinfos(addrinfos, first_address_family_count=1):
163 """Interleave list of addrinfo tuples by family."""
164 # Group addresses by family
165 addrinfos_by_family = collections.OrderedDict()
166 for addr in addrinfos:
167 family = addr[0]
168 if family not in addrinfos_by_family:
169 addrinfos_by_family[family] = []
170 addrinfos_by_family[family].append(addr)
171 addrinfos_lists = list(addrinfos_by_family.values())
173 reordered = []
174 if first_address_family_count > 1:
175 reordered.extend(addrinfos_lists[0][:first_address_family_count - 1])
176 del addrinfos_lists[0][:first_address_family_count - 1]
177 reordered.extend(
178 a for a in itertools.chain.from_iterable(
179 itertools.zip_longest(*addrinfos_lists)
180 ) if a is not None)
181 return reordered
184def _run_until_complete_cb(fut):
185 if not fut.cancelled():
186 exc = fut.exception()
187 if isinstance(exc, (SystemExit, KeyboardInterrupt)):
188 # Issue #22429: run_forever() already finished, no need to
189 # stop it.
190 return
191 futures._get_loop(fut).stop()
194if hasattr(socket, 'TCP_NODELAY'): 194 ↛ 207line 194 didn't jump to line 207 because the condition on line 194 was always true
195 def _set_nodelay(sock):
196 if (sock.family in {socket.AF_INET, socket.AF_INET6} and
197 sock.type == socket.SOCK_STREAM and
198 sock.proto == socket.IPPROTO_TCP):
199 try:
200 sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
201 except OSError:
202 # TCP_NODELAY is only a latency optimization. It can fail
203 # on a connection already reset by the peer (e.g. EINVAL
204 # on macOS); the error will surface on the next I/O.
205 pass
206else:
207 def _set_nodelay(sock):
208 pass
211def _check_ssl_socket(sock):
212 if ssl is not None and isinstance(sock, ssl.SSLSocket): 212 ↛ 213line 212 didn't jump to line 213 because the condition on line 212 was never true
213 raise TypeError("Socket cannot be of type SSLSocket")
216class _SendfileFallbackProtocol(protocols.Protocol):
217 def __init__(self, transp):
218 if not isinstance(transp, transports._FlowControlMixin): 218 ↛ 219line 218 didn't jump to line 219 because the condition on line 218 was never true
219 raise TypeError("transport should be _FlowControlMixin instance")
220 self._transport = transp
221 self._proto = transp.get_protocol()
222 self._should_resume_reading = transp.is_reading()
223 self._should_resume_writing = transp._protocol_paused
224 transp.pause_reading()
225 transp.set_protocol(self)
226 if self._should_resume_writing: 226 ↛ 227line 226 didn't jump to line 227 because the condition on line 226 was never true
227 self._write_ready_fut = self._transport._loop.create_future()
228 else:
229 self._write_ready_fut = None
231 async def drain(self):
232 if self._transport.is_closing(): 232 ↛ 233line 232 didn't jump to line 233 because the condition on line 232 was never true
233 raise ConnectionError("Connection closed by peer")
234 fut = self._write_ready_fut
235 if fut is None:
236 return
237 await fut
239 def connection_made(self, transport):
240 raise RuntimeError("Invalid state: "
241 "connection should have been established already.")
243 def connection_lost(self, exc):
244 if self._write_ready_fut is not None: 244 ↛ 252line 244 didn't jump to line 252 because the condition on line 244 was always true
245 # Never happens if peer disconnects after sending the whole content
246 # Thus disconnection is always an exception from user perspective
247 if exc is None: 247 ↛ 248line 247 didn't jump to line 248 because the condition on line 247 was never true
248 self._write_ready_fut.set_exception(
249 ConnectionError("Connection is closed by peer"))
250 else:
251 self._write_ready_fut.set_exception(exc)
252 self._proto.connection_lost(exc)
254 def pause_writing(self):
255 if self._write_ready_fut is not None: 255 ↛ 256line 255 didn't jump to line 256 because the condition on line 255 was never true
256 return
257 self._write_ready_fut = self._transport._loop.create_future()
259 def resume_writing(self):
260 if self._write_ready_fut is None: 260 ↛ 261line 260 didn't jump to line 261 because the condition on line 260 was never true
261 return
262 self._write_ready_fut.set_result(False)
263 self._write_ready_fut = None
265 def data_received(self, data):
266 raise RuntimeError("Invalid state: reading should be paused")
268 def eof_received(self):
269 raise RuntimeError("Invalid state: reading should be paused")
271 async def restore(self):
272 self._transport.set_protocol(self._proto)
273 if self._should_resume_reading: 273 ↛ 275line 273 didn't jump to line 275 because the condition on line 273 was always true
274 self._transport.resume_reading()
275 if self._write_ready_fut is not None:
276 # Cancel the future.
277 # Basically it has no effect because protocol is switched back,
278 # no code should wait for it anymore.
279 self._write_ready_fut.cancel()
280 if self._should_resume_writing: 280 ↛ 281line 280 didn't jump to line 281 because the condition on line 280 was never true
281 self._proto.resume_writing()
284class Server(events.AbstractServer):
286 def __init__(self, loop, sockets, protocol_factory, ssl_context, backlog,
287 ssl_handshake_timeout, ssl_shutdown_timeout=None):
288 self._loop = loop
289 self._sockets = sockets
290 # Weak references so we don't break Transport's ability to
291 # detect abandoned transports
292 self._clients = weakref.WeakSet()
293 self._waiters = []
294 self._protocol_factory = protocol_factory
295 self._backlog = backlog
296 self._ssl_context = ssl_context
297 self._ssl_handshake_timeout = ssl_handshake_timeout
298 self._ssl_shutdown_timeout = ssl_shutdown_timeout
299 self._serving = False
300 self._serving_forever_fut = None
301 self._context = contextvars.copy_context()
303 def __repr__(self):
304 return f'<{self.__class__.__name__} sockets={self.sockets!r}>'
306 def _attach(self, transport):
307 assert self._sockets is not None
308 self._clients.add(transport)
310 def _detach(self, transport):
311 self._clients.discard(transport)
312 if len(self._clients) == 0 and self._sockets is None:
313 self._wakeup()
315 def _wakeup(self):
316 waiters = self._waiters
317 self._waiters = None
318 for waiter in waiters:
319 if not waiter.done(): 319 ↛ 318line 319 didn't jump to line 318 because the condition on line 319 was always true
320 waiter.set_result(None)
322 def _start_serving(self):
323 if self._serving:
324 return
325 self._serving = True
326 for sock in self._sockets:
327 sock.listen(self._backlog)
328 self._loop._start_serving(
329 self._protocol_factory, sock, self._ssl_context,
330 self, self._backlog, self._ssl_handshake_timeout,
331 self._ssl_shutdown_timeout, context=self._context)
333 def get_loop(self):
334 return self._loop
336 def is_serving(self):
337 return self._serving
339 @property
340 def sockets(self):
341 if self._sockets is None:
342 return ()
343 return tuple(trsock.TransportSocket(s) for s in self._sockets)
345 def close(self):
346 sockets = self._sockets
347 if sockets is None:
348 return
349 self._sockets = None
351 for sock in sockets:
352 self._loop._stop_serving(sock)
354 self._serving = False
356 if (self._serving_forever_fut is not None and 356 ↛ 358line 356 didn't jump to line 358 because the condition on line 356 was never true
357 not self._serving_forever_fut.done()):
358 self._serving_forever_fut.cancel()
359 self._serving_forever_fut = None
361 if len(self._clients) == 0:
362 self._wakeup()
364 def close_clients(self):
365 for transport in self._clients.copy():
366 transport.close()
368 def abort_clients(self):
369 for transport in self._clients.copy():
370 transport.abort()
372 async def start_serving(self):
373 self._start_serving()
374 # Skip one loop iteration so that all 'loop.add_reader'
375 # go through.
376 await tasks.sleep(0)
378 async def serve_forever(self):
379 if self._serving_forever_fut is not None: 379 ↛ 380line 379 didn't jump to line 380 because the condition on line 379 was never true
380 raise RuntimeError(
381 f'server {self!r} is already being awaited on serve_forever()')
382 if self._sockets is None:
383 raise RuntimeError(f'server {self!r} is closed')
385 self._start_serving()
386 self._serving_forever_fut = self._loop.create_future()
388 try:
389 await self._serving_forever_fut
390 except exceptions.CancelledError:
391 try:
392 self.close()
393 self.close_clients()
394 await self.wait_closed()
395 finally:
396 raise
397 finally:
398 self._serving_forever_fut = None
400 async def wait_closed(self):
401 """Wait until server is closed and all connections are dropped.
403 - If the server is not closed, wait.
404 - If it is closed, but there are still active connections, wait.
406 Anyone waiting here will be unblocked once both conditions
407 (server is closed and all connections have been dropped)
408 have become true, in either order.
410 Historical note: In 3.11 and before, this was broken, returning
411 immediately if the server was already closed, even if there
412 were still active connections. An attempted fix in 3.12.0 was
413 still broken, returning immediately if the server was still
414 open and there were no active connections. Hopefully in 3.12.1
415 we have it right.
416 """
417 # Waiters are unblocked by self._wakeup(), which is called
418 # from two places: self.close() and self._detach(), but only
419 # when both conditions have become true. To signal that this
420 # has happened, self._wakeup() sets self._waiters to None.
421 if self._waiters is None:
422 return
423 waiter = self._loop.create_future()
424 self._waiters.append(waiter)
425 await waiter
428class BaseEventLoop(events.AbstractEventLoop):
430 def __init__(self):
431 self._timer_cancelled_count = 0
432 self._closed = False
433 self._stopping = False
434 self._ready = collections.deque()
435 self._scheduled = []
436 self._default_executor = None
437 self._internal_fds = 0
438 # Identifier of the thread running the event loop, or None if the
439 # event loop is not running
440 self._thread_id = None
441 self._exception_handler = None
442 self.set_debug(coroutines._is_debug_mode())
443 # The preserved state of async generator hooks.
444 self._old_agen_hooks = None
445 # In debug mode, if the execution of a callback or a step of a task
446 # exceed this duration in seconds, the slow callback/task is logged.
447 self.slow_callback_duration = 0.1
448 self._current_handle = None
449 self._task_factory = None
450 self._coroutine_origin_tracking_enabled = False
451 self._coroutine_origin_tracking_saved_depth = None
453 # A weak set of all asynchronous generators that are
454 # being iterated by the loop.
455 self._asyncgens = weakref.WeakSet()
456 # Set to True when `loop.shutdown_asyncgens` is called.
457 self._asyncgens_shutdown_called = False
458 # Set to True when `loop.shutdown_default_executor` is called.
459 self._executor_shutdown_called = False
461 def __repr__(self):
462 return (
463 f'<{self.__class__.__name__} running={self.is_running()} '
464 f'closed={self.is_closed()} debug={self.get_debug()}>'
465 )
467 def create_future(self):
468 """Create a Future object attached to the loop."""
469 return futures.Future(loop=self)
471 def create_task(self, coro, **kwargs):
472 """Schedule or begin executing a coroutine object.
474 Return a task object.
475 """
476 self._check_closed()
477 if self._task_factory is not None:
478 return self._task_factory(self, coro, **kwargs)
480 task = tasks.Task(coro, loop=self, **kwargs)
481 if task._source_traceback:
482 del task._source_traceback[-1]
483 try:
484 return task
485 finally:
486 # gh-128552: prevent a refcycle of
487 # task.exception().__traceback__->BaseEventLoop.create_task->task
488 del task
490 def set_task_factory(self, factory):
491 """Set a task factory that will be used by loop.create_task().
493 If factory is None the default task factory will be set.
495 If factory is a callable, it should have a signature matching
496 '(loop, coro, **kwargs)', where 'loop' will be a reference to the
497 active event loop, 'coro' will be a coroutine object, and **kwargs
498 will be arbitrary keyword arguments that should be passed on to
499 Task. The callable must return a Task.
500 """
501 if factory is not None and not callable(factory):
502 raise TypeError('task factory must be a callable or None')
503 self._task_factory = factory
505 def get_task_factory(self):
506 """Return a task factory, or None if the default one is in use."""
507 return self._task_factory
509 def _make_socket_transport(self, sock, protocol, waiter=None, *,
510 extra=None, server=None):
511 """Create socket transport."""
512 raise NotImplementedError
514 def _make_ssl_transport(
515 self, rawsock, protocol, sslcontext, waiter=None,
516 *, server_side=False, server_hostname=None,
517 extra=None, server=None,
518 ssl_handshake_timeout=None,
519 ssl_shutdown_timeout=None,
520 call_connection_made=True,
521 context=None):
522 """Create SSL transport."""
523 raise NotImplementedError
525 def _make_datagram_transport(self, sock, protocol,
526 address=None, waiter=None, extra=None):
527 """Create datagram transport."""
528 raise NotImplementedError
530 def _make_read_pipe_transport(self, pipe, protocol, waiter=None,
531 extra=None):
532 """Create read pipe transport."""
533 raise NotImplementedError
535 def _make_write_pipe_transport(self, pipe, protocol, waiter=None,
536 extra=None):
537 """Create write pipe transport."""
538 raise NotImplementedError
540 async def _make_subprocess_transport(self, protocol, args, shell,
541 stdin, stdout, stderr, bufsize,
542 extra=None, **kwargs):
543 """Create subprocess transport."""
544 raise NotImplementedError
546 def _write_to_self(self):
547 """Write a byte to self-pipe, to wake up the event loop.
549 This may be called from a different thread.
551 The subclass is responsible for implementing the self-pipe.
552 """
553 raise NotImplementedError
555 def _process_events(self, event_list):
556 """Process selector events."""
557 raise NotImplementedError
559 def _check_closed(self):
560 if self._closed:
561 raise RuntimeError('Event loop is closed')
563 def _check_default_executor(self):
564 if self._executor_shutdown_called: 564 ↛ 565line 564 didn't jump to line 565 because the condition on line 564 was never true
565 raise RuntimeError('Executor shutdown has been called')
567 def _asyncgen_finalizer_hook(self, agen):
568 self._asyncgens.discard(agen)
569 if not self.is_closed(): 569 ↛ exitline 569 didn't return from function '_asyncgen_finalizer_hook' because the condition on line 569 was always true
570 self.call_soon_threadsafe(self.create_task, agen.aclose())
572 def _asyncgen_firstiter_hook(self, agen):
573 if self._asyncgens_shutdown_called: 573 ↛ 574line 573 didn't jump to line 574 because the condition on line 573 was never true
574 warnings.warn(
575 f"asynchronous generator {agen!r} was scheduled after "
576 f"loop.shutdown_asyncgens() call",
577 ResourceWarning, source=self)
579 self._asyncgens.add(agen)
581 async def shutdown_asyncgens(self):
582 """Shutdown all active asynchronous generators."""
583 self._asyncgens_shutdown_called = True
585 if not len(self._asyncgens):
586 # If Python version is <3.6 or we don't have any asynchronous
587 # generators alive.
588 return
590 closing_agens = list(self._asyncgens)
591 self._asyncgens.clear()
593 results = await tasks.gather(
594 *[ag.aclose() for ag in closing_agens],
595 return_exceptions=True)
597 for result, agen in zip(results, closing_agens):
598 if isinstance(result, BaseException): 598 ↛ 597line 598 didn't jump to line 597 because the condition on line 598 was always true
599 self.call_exception_handler({
600 'message': f'an error occurred during closing of '
601 f'asynchronous generator {agen!r}',
602 'exception': result,
603 'asyncgen': agen
604 })
606 async def shutdown_default_executor(self, timeout=None):
607 """Schedule the shutdown of the default executor.
609 The timeout parameter specifies the amount of time the executor will
610 be given to finish joining. The default value is None, which means
611 that the executor will be given an unlimited amount of time.
612 """
613 self._executor_shutdown_called = True
614 if self._default_executor is None:
615 return
616 future = self.create_future()
617 thread = threading.Thread(target=self._do_shutdown, args=(future,))
618 thread.start()
619 try:
620 async with timeouts.timeout(timeout):
621 await future
622 except TimeoutError:
623 warnings.warn("The executor did not finishing joining "
624 f"its threads within {timeout} seconds.",
625 RuntimeWarning, stacklevel=2)
626 self._default_executor.shutdown(wait=False)
627 else:
628 thread.join()
630 def _do_shutdown(self, future):
631 try:
632 self._default_executor.shutdown(wait=True)
633 if not self.is_closed(): 633 ↛ exitline 633 didn't return from function '_do_shutdown' because the condition on line 633 was always true
634 self.call_soon_threadsafe(futures._set_result_unless_cancelled,
635 future, None)
636 except Exception as ex:
637 if not self.is_closed() and not future.cancelled():
638 self.call_soon_threadsafe(future.set_exception, ex)
640 def _check_running(self):
641 if self.is_running():
642 raise RuntimeError('This event loop is already running')
643 if events._get_running_loop() is not None:
644 raise RuntimeError(
645 'Cannot run the event loop while another loop is running')
647 def _run_forever_setup(self):
648 """Prepare the run loop to process events.
650 This method exists so that custom event loop subclasses (e.g., event loops
651 that integrate a GUI event loop with Python's event loop) have access to all the
652 loop setup logic.
653 """
654 self._check_closed()
655 self._check_running()
656 self._set_coroutine_origin_tracking(self._debug)
658 self._old_agen_hooks = sys.get_asyncgen_hooks()
659 self._thread_id = threading.get_ident()
660 sys.set_asyncgen_hooks(
661 firstiter=self._asyncgen_firstiter_hook,
662 finalizer=self._asyncgen_finalizer_hook
663 )
665 events._set_running_loop(self)
667 def _run_forever_cleanup(self):
668 """Clean up after an event loop finishes the looping over events.
670 This method exists so that custom event loop subclasses (e.g., event loops
671 that integrate a GUI event loop with Python's event loop) have access to all the
672 loop cleanup logic.
673 """
674 self._stopping = False
675 self._thread_id = None
676 events._set_running_loop(None)
677 self._set_coroutine_origin_tracking(False)
678 # Restore any pre-existing async generator hooks.
679 if self._old_agen_hooks is not None: 679 ↛ exitline 679 didn't return from function '_run_forever_cleanup' because the condition on line 679 was always true
680 sys.set_asyncgen_hooks(*self._old_agen_hooks)
681 self._old_agen_hooks = None
683 def run_forever(self):
684 """Run until stop() is called."""
685 self._run_forever_setup()
686 try:
687 while True:
688 self._run_once()
689 if self._stopping:
690 break
691 finally:
692 self._run_forever_cleanup()
694 def run_until_complete(self, future):
695 """Run until the Future is done.
697 If the argument is a coroutine, it is wrapped in a Task.
699 WARNING: It would be disastrous to call run_until_complete()
700 with the same coroutine twice -- it would wrap it in two
701 different Tasks and that can't be good.
703 Return the Future's result, or raise its exception.
704 """
705 self._check_closed()
706 self._check_running()
708 new_task = not futures.isfuture(future)
709 future = tasks.ensure_future(future, loop=self)
710 if new_task:
711 # An exception is raised if the future didn't complete, so there
712 # is no need to log the "destroy pending task" message
713 future._log_destroy_pending = False
715 future.add_done_callback(_run_until_complete_cb)
716 try:
717 self.run_forever()
718 except:
719 if new_task and future.done() and not future.cancelled():
720 # The coroutine raised a BaseException. Consume the exception
721 # to not log a warning, the caller doesn't have access to the
722 # local task.
723 future.exception()
724 raise
725 finally:
726 future.remove_done_callback(_run_until_complete_cb)
727 if not future.done():
728 raise RuntimeError('Event loop stopped before Future completed.')
730 return future.result()
732 def stop(self):
733 """Stop running the event loop.
735 Every callback already scheduled will still run. This simply
736 informs run_forever to stop looping after a complete iteration.
737 """
738 self._stopping = True
740 def close(self):
741 """Close the event loop.
743 This clears the queues and shuts down the executor,
744 but does not wait for the executor to finish.
746 The event loop must not be running.
747 """
748 if self.is_running(): 748 ↛ 749line 748 didn't jump to line 749 because the condition on line 748 was never true
749 raise RuntimeError("Cannot close a running event loop")
750 if self._closed:
751 return
752 if self._debug:
753 logger.debug("Close %r", self)
754 self._closed = True
755 self._ready.clear()
756 self._scheduled.clear()
757 self._executor_shutdown_called = True
758 executor = self._default_executor
759 if executor is not None:
760 self._default_executor = None
761 executor.shutdown(wait=False)
763 def is_closed(self):
764 """Returns True if the event loop was closed."""
765 return self._closed
767 def __del__(self, _warn=warnings.warn):
768 if not self.is_closed(): 768 ↛ 769line 768 didn't jump to line 769 because the condition on line 768 was never true
769 _warn(f"unclosed event loop {self!r}", ResourceWarning, source=self)
770 if not self.is_running():
771 self.close()
773 def is_running(self):
774 """Returns True if the event loop is running."""
775 return (self._thread_id is not None)
777 def time(self):
778 """Return the time according to the event loop's clock.
780 This is a float expressed in seconds since an epoch, but the
781 epoch, precision, accuracy and drift are unspecified and may
782 differ per event loop.
783 """
784 return time.monotonic()
786 def call_later(self, delay, callback, *args, context=None):
787 """Arrange for a callback to be called at a given time.
789 Return a Handle: an opaque object with a cancel() method that
790 can be used to cancel the call.
792 The delay can be an int or float, expressed in seconds. It is
793 always relative to the current time.
795 Each callback will be called exactly once. If two callbacks
796 are scheduled for exactly the same time, it is undefined which
797 will be called first.
799 Any positional arguments after the callback will be passed to
800 the callback when it is called.
801 """
802 if delay is None:
803 raise TypeError('delay must not be None')
804 timer = self.call_at(self.time() + delay, callback, *args,
805 context=context)
806 if timer._source_traceback:
807 del timer._source_traceback[-1]
808 return timer
810 def call_at(self, when, callback, *args, context=None):
811 """Like call_later(), but uses an absolute time.
813 Absolute time corresponds to the event loop's time() method.
814 """
815 if when is None:
816 raise TypeError("when cannot be None")
817 self._check_closed()
818 if self._debug:
819 self._check_thread()
820 self._check_callback(callback, 'call_at')
821 timer = events.TimerHandle(when, callback, args, self, context)
822 if timer._source_traceback:
823 del timer._source_traceback[-1]
824 heapq.heappush(self._scheduled, timer)
825 timer._scheduled = True
826 return timer
828 def call_soon(self, callback, *args, context=None):
829 """Arrange for a callback to be called as soon as possible.
831 This operates as a FIFO queue: callbacks are called in the
832 order in which they are registered. Each callback will be
833 called exactly once.
835 Any positional arguments after the callback will be passed to
836 the callback when it is called.
837 """
838 self._check_closed()
839 if self._debug:
840 self._check_thread()
841 self._check_callback(callback, 'call_soon')
842 handle = self._call_soon(callback, args, context)
843 if handle._source_traceback:
844 del handle._source_traceback[-1]
845 return handle
847 def _check_callback(self, callback, method):
848 if (coroutines.iscoroutine(callback) or
849 inspect.iscoroutinefunction(callback)):
850 raise TypeError(
851 f"coroutines cannot be used with {method}()")
852 if not callable(callback):
853 raise TypeError(
854 f'a callable object was expected by {method}(), '
855 f'got {callback!r}')
857 def _call_soon(self, callback, args, context):
858 handle = events.Handle(callback, args, self, context)
859 if handle._source_traceback:
860 del handle._source_traceback[-1]
861 self._ready.append(handle)
862 return handle
864 def _check_thread(self):
865 """Check that the current thread is the thread running the event loop.
867 Non-thread-safe methods of this class make this assumption and will
868 likely behave incorrectly when the assumption is violated.
870 Should only be called when (self._debug == True). The caller is
871 responsible for checking this condition for performance reasons.
872 """
873 if self._thread_id is None:
874 return
875 thread_id = threading.get_ident()
876 if thread_id != self._thread_id:
877 raise RuntimeError(
878 "Non-thread-safe operation invoked on an event loop other "
879 "than the current one")
881 def call_soon_threadsafe(self, callback, *args, context=None):
882 """Like call_soon(), but thread-safe."""
883 self._check_closed()
884 if self._debug:
885 self._check_callback(callback, 'call_soon_threadsafe')
886 handle = events._ThreadSafeHandle(callback, args, self, context)
887 self._ready.append(handle)
888 if handle._source_traceback:
889 del handle._source_traceback[-1]
890 if handle._source_traceback:
891 del handle._source_traceback[-1]
892 self._write_to_self()
893 return handle
895 def run_in_executor(self, executor, func, *args):
896 self._check_closed()
897 if self._debug:
898 self._check_callback(func, 'run_in_executor')
899 if executor is None:
900 executor = self._default_executor
901 # Only check when the default executor is being used
902 self._check_default_executor()
903 if executor is None:
904 executor = concurrent.futures.ThreadPoolExecutor(
905 thread_name_prefix='asyncio'
906 )
907 self._default_executor = executor
908 return futures.wrap_future(
909 executor.submit(func, *args), loop=self)
911 def set_default_executor(self, executor):
912 if not isinstance(executor, concurrent.futures.ThreadPoolExecutor):
913 raise TypeError('executor must be ThreadPoolExecutor instance')
914 self._default_executor = executor
916 def _getaddrinfo_debug(self, host, port, family, type, proto, flags):
917 msg = [f"{host}:{port!r}"]
918 if family:
919 msg.append(f'family={family!r}')
920 if type:
921 msg.append(f'type={type!r}')
922 if proto:
923 msg.append(f'proto={proto!r}')
924 if flags:
925 msg.append(f'flags={flags!r}')
926 msg = ', '.join(msg)
927 logger.debug('Get address info %s', msg)
929 t0 = self.time()
930 addrinfo = socket.getaddrinfo(host, port, family, type, proto, flags)
931 dt = self.time() - t0
933 msg = f'Getting address info {msg} took {dt * 1e3:.3f}ms: {addrinfo!r}'
934 if dt >= self.slow_callback_duration:
935 logger.info(msg)
936 else:
937 logger.debug(msg)
938 return addrinfo
940 async def getaddrinfo(self, host, port, *,
941 family=0, type=0, proto=0, flags=0):
942 if self._debug: 942 ↛ 943line 942 didn't jump to line 943 because the condition on line 942 was never true
943 getaddr_func = self._getaddrinfo_debug
944 else:
945 getaddr_func = socket.getaddrinfo
947 return await self.run_in_executor(
948 None, getaddr_func, host, port, family, type, proto, flags)
950 async def getnameinfo(self, sockaddr, flags=0):
951 return await self.run_in_executor(
952 None, socket.getnameinfo, sockaddr, flags)
954 async def sock_sendfile(self, sock, file, offset=0, count=None,
955 *, fallback=True):
956 if self._debug and sock.gettimeout() != 0:
957 raise ValueError("the socket must be non-blocking")
958 _check_ssl_socket(sock)
959 self._check_sendfile_params(sock, file, offset, count)
960 try:
961 return await self._sock_sendfile_native(sock, file,
962 offset, count)
963 except exceptions.SendfileNotAvailableError:
964 if not fallback:
965 raise
966 return await self._sock_sendfile_fallback(sock, file,
967 offset, count)
969 async def _sock_sendfile_native(self, sock, file, offset, count):
970 # NB: sendfile syscall is not supported for SSL sockets and
971 # non-mmap files even if sendfile is supported by OS
972 raise exceptions.SendfileNotAvailableError(
973 f"syscall sendfile is not available for socket {sock!r} "
974 f"and file {file!r} combination")
976 async def _sock_sendfile_fallback(self, sock, file, offset, count):
977 if hasattr(file, 'seek'): 977 ↛ 979line 977 didn't jump to line 979 because the condition on line 977 was always true
978 file.seek(offset)
979 blocksize = (
980 min(count, constants.SENDFILE_FALLBACK_READBUFFER_SIZE)
981 if count else constants.SENDFILE_FALLBACK_READBUFFER_SIZE
982 )
983 buf = bytearray(blocksize)
984 total_sent = 0
985 try:
986 while True:
987 if count:
988 blocksize = min(count - total_sent, blocksize)
989 if blocksize <= 0:
990 break
991 view = memoryview(buf)[:blocksize]
992 read = await self.run_in_executor(None, file.readinto, view)
993 if not read:
994 break # EOF
995 await self.sock_sendall(sock, view[:read])
996 total_sent += read
997 return total_sent
998 finally:
999 if total_sent > 0 and hasattr(file, 'seek'):
1000 file.seek(offset + total_sent)
1002 def _check_sendfile_params(self, sock, file, offset, count):
1003 if 'b' not in getattr(file, 'mode', 'b'):
1004 raise ValueError("file should be opened in binary mode")
1005 if not sock.type == socket.SOCK_STREAM:
1006 raise ValueError("only SOCK_STREAM type sockets are supported")
1007 if count is not None:
1008 if not isinstance(count, int):
1009 raise TypeError(
1010 "count must be a positive integer (got {!r})".format(count))
1011 if count <= 0:
1012 raise ValueError(
1013 "count must be a positive integer (got {!r})".format(count))
1014 if not isinstance(offset, int):
1015 raise TypeError(
1016 "offset must be a non-negative integer (got {!r})".format(
1017 offset))
1018 if offset < 0:
1019 raise ValueError(
1020 "offset must be a non-negative integer (got {!r})".format(
1021 offset))
1023 async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
1024 """Create, bind and connect one socket."""
1025 my_exceptions = []
1026 exceptions.append(my_exceptions)
1027 family, type_, proto, _, address = addr_info
1028 sock = None
1029 try:
1030 try:
1031 sock = socket.socket(family=family, type=type_, proto=proto)
1032 sock.setblocking(False)
1033 if local_addr_infos is not None:
1034 for lfamily, _, _, _, laddr in local_addr_infos:
1035 # skip local addresses of different family
1036 if lfamily != family:
1037 continue
1038 try:
1039 sock.bind(laddr)
1040 break
1041 except OSError as exc:
1042 msg = (
1043 f'error while attempting to bind on '
1044 f'address {laddr!r}: {str(exc).lower()}'
1045 )
1046 exc = OSError(exc.errno, msg)
1047 my_exceptions.append(exc)
1048 else: # all bind attempts failed
1049 if my_exceptions:
1050 raise my_exceptions.pop()
1051 else:
1052 raise OSError(f"no matching local address with {family=} found")
1053 await self.sock_connect(sock, address)
1054 return sock
1055 except OSError as exc:
1056 my_exceptions.append(exc)
1057 raise
1058 except:
1059 if sock is not None:
1060 try:
1061 sock.close()
1062 except OSError:
1063 # An error when closing a newly created socket is
1064 # not important, but it can overwrite more important
1065 # non-OSError error. So ignore it.
1066 pass
1067 raise
1068 finally:
1069 exceptions = my_exceptions = None
1071 async def create_connection(
1072 self, protocol_factory, host=None, port=None,
1073 *, ssl=None, family=0,
1074 proto=0, flags=0, sock=None,
1075 local_addr=None, server_hostname=None,
1076 ssl_handshake_timeout=None,
1077 ssl_shutdown_timeout=None,
1078 happy_eyeballs_delay=None, interleave=None,
1079 all_errors=False):
1080 """Connect to a TCP server.
1082 Create a streaming transport connection to a given internet host and
1083 port: socket family AF_INET or socket.AF_INET6 depending on host (or
1084 family if specified), socket type SOCK_STREAM. protocol_factory must
1085 be a callable returning a protocol instance.
1087 This method is a coroutine which will try to establish the
1088 connection in the background. When successful, the coroutine
1089 returns a (transport, protocol) pair.
1090 """
1091 if server_hostname is not None and not ssl:
1092 raise ValueError('server_hostname is only meaningful with ssl')
1094 if server_hostname is None and ssl:
1095 # Use host as default for server_hostname. It is an error
1096 # if host is empty or not set, e.g. when an
1097 # already-connected socket was passed or when only a port
1098 # is given. To avoid this error, you can pass
1099 # server_hostname='' -- this will bypass the hostname
1100 # check. (This also means that if host is a numeric
1101 # IP/IPv6 address, we will attempt to verify that exact
1102 # address; this will probably fail, but it is possible to
1103 # create a certificate for a specific IP address, so we
1104 # don't judge it here.)
1105 if not host:
1106 raise ValueError('You must set server_hostname '
1107 'when using ssl without a host')
1108 server_hostname = host
1110 if ssl_handshake_timeout is not None and not ssl:
1111 raise ValueError(
1112 'ssl_handshake_timeout is only meaningful with ssl')
1114 if ssl_shutdown_timeout is not None and not ssl: 1114 ↛ 1115line 1114 didn't jump to line 1115 because the condition on line 1114 was never true
1115 raise ValueError(
1116 'ssl_shutdown_timeout is only meaningful with ssl')
1118 if sock is not None:
1119 _check_ssl_socket(sock)
1121 if happy_eyeballs_delay is not None and interleave is None:
1122 # If using happy eyeballs, default to interleave addresses by family
1123 interleave = 1
1125 if host is not None or port is not None:
1126 if sock is not None:
1127 raise ValueError(
1128 'host/port and sock can not be specified at the same time')
1130 infos = await self._ensure_resolved(
1131 (host, port), family=family,
1132 type=socket.SOCK_STREAM, proto=proto, flags=flags, loop=self)
1133 if not infos:
1134 raise OSError('getaddrinfo() returned empty list')
1136 if local_addr is not None:
1137 laddr_infos = await self._ensure_resolved(
1138 local_addr, family=family,
1139 type=socket.SOCK_STREAM, proto=proto,
1140 flags=flags, loop=self)
1141 if not laddr_infos:
1142 raise OSError('getaddrinfo() returned empty list')
1143 else:
1144 laddr_infos = None
1146 if interleave:
1147 infos = _interleave_addrinfos(infos, interleave)
1149 exceptions = []
1150 if happy_eyeballs_delay is None:
1151 # not using happy eyeballs
1152 for addrinfo in infos:
1153 try:
1154 sock = await self._connect_sock(
1155 exceptions, addrinfo, laddr_infos)
1156 break
1157 except OSError:
1158 continue
1159 else: # using happy eyeballs
1160 sock = (await staggered.staggered_race(
1161 (
1162 # can't use functools.partial as it keeps a reference
1163 # to exceptions
1164 lambda addrinfo=addrinfo: self._connect_sock(
1165 exceptions, addrinfo, laddr_infos
1166 )
1167 for addrinfo in infos
1168 ),
1169 happy_eyeballs_delay,
1170 loop=self,
1171 ))[0] # can't use sock, _, _ as it keeks a reference to exceptions
1173 if sock is None:
1174 exceptions = [exc for sub in exceptions for exc in sub]
1175 try:
1176 if all_errors:
1177 raise ExceptionGroup("create_connection failed", exceptions)
1178 if len(exceptions) == 1:
1179 raise exceptions[0]
1180 elif exceptions:
1181 # If they all have the same str(), raise one.
1182 model = str(exceptions[0])
1183 if all(str(exc) == model for exc in exceptions):
1184 raise exceptions[0]
1185 # Raise a combined exception so the user can see all
1186 # the various error messages.
1187 raise OSError('Multiple exceptions: {}'.format(
1188 ', '.join(str(exc) for exc in exceptions)))
1189 else:
1190 # No exceptions were collected, raise a timeout error
1191 raise TimeoutError('create_connection failed')
1192 finally:
1193 exceptions = None
1195 else:
1196 if sock is None:
1197 raise ValueError(
1198 'host and port was not specified and no sock specified')
1199 if sock.type != socket.SOCK_STREAM:
1200 # We allow AF_INET, AF_INET6, AF_UNIX as long as they
1201 # are SOCK_STREAM.
1202 # We support passing AF_UNIX sockets even though we have
1203 # a dedicated API for that: create_unix_connection.
1204 # Disallowing AF_UNIX in this method, breaks backwards
1205 # compatibility.
1206 raise ValueError(
1207 f'A Stream Socket was expected, got {sock!r}')
1209 transport, protocol = await self._create_connection_transport(
1210 sock, protocol_factory, ssl, server_hostname,
1211 ssl_handshake_timeout=ssl_handshake_timeout,
1212 ssl_shutdown_timeout=ssl_shutdown_timeout)
1213 if self._debug:
1214 # Get the socket from the transport because SSL transport closes
1215 # the old socket and creates a new SSL socket
1216 sock = transport.get_extra_info('socket')
1217 logger.debug("%r connected to %s:%r: (%r, %r)",
1218 sock, host, port, transport, protocol)
1219 return transport, protocol
1221 async def _create_connection_transport(
1222 self, sock, protocol_factory, ssl,
1223 server_hostname, server_side=False,
1224 ssl_handshake_timeout=None,
1225 ssl_shutdown_timeout=None, context=None):
1227 try:
1228 sock.setblocking(False)
1229 context = context if context is not None else contextvars.copy_context()
1231 protocol = protocol_factory()
1232 waiter = self.create_future()
1233 if ssl:
1234 sslcontext = None if isinstance(ssl, bool) else ssl
1235 transport = self._make_ssl_transport(
1236 sock, protocol, sslcontext, waiter,
1237 server_side=server_side, server_hostname=server_hostname,
1238 ssl_handshake_timeout=ssl_handshake_timeout,
1239 ssl_shutdown_timeout=ssl_shutdown_timeout,
1240 context=context)
1241 else:
1242 transport = self._make_socket_transport(sock, protocol, waiter, context=context)
1243 except:
1244 # gh-153133: close the socket if the transport is never created.
1245 sock.close()
1246 raise
1248 try:
1249 await waiter
1250 except:
1251 transport.close()
1252 waiter = None
1253 raise
1255 return transport, protocol
1257 async def sendfile(self, transport, file, offset=0, count=None,
1258 *, fallback=True):
1259 """Send a file to transport.
1261 Return the total number of bytes which were sent.
1263 The method uses high-performance os.sendfile if available.
1265 file must be a regular file object opened in binary mode.
1267 offset tells from where to start reading the file. If specified,
1268 count is the total number of bytes to transmit as opposed to
1269 sending the file until EOF is reached. File position is updated on
1270 return or also in case of error in which case file.tell()
1271 can be used to figure out the number of bytes
1272 which were sent.
1274 fallback set to True makes asyncio to manually read and send
1275 the file when the platform does not support the sendfile syscall
1276 (e.g. Windows or SSL socket on Unix).
1278 Raise SendfileNotAvailableError if the system does not support
1279 sendfile syscall and fallback is False.
1280 """
1281 if transport.is_closing():
1282 raise RuntimeError("Transport is closing")
1283 mode = getattr(transport, '_sendfile_compatible',
1284 constants._SendfileMode.UNSUPPORTED)
1285 if mode is constants._SendfileMode.UNSUPPORTED:
1286 raise RuntimeError(
1287 f"sendfile is not supported for transport {transport!r}")
1288 if mode is constants._SendfileMode.TRY_NATIVE:
1289 try:
1290 return await self._sendfile_native(transport, file,
1291 offset, count)
1292 except exceptions.SendfileNotAvailableError:
1293 if not fallback:
1294 raise
1296 if not fallback:
1297 raise exceptions.SendfileNotAvailableError(
1298 f"fallback is disabled and native sendfile is not "
1299 f"supported for transport {transport!r}")
1300 return await self._sendfile_fallback(transport, file,
1301 offset, count)
1303 async def _sendfile_native(self, transp, file, offset, count):
1304 raise exceptions.SendfileNotAvailableError(
1305 "sendfile syscall is not supported")
1307 async def _sendfile_fallback(self, transp, file, offset, count):
1308 if hasattr(file, 'seek'): 1308 ↛ 1310line 1308 didn't jump to line 1310 because the condition on line 1308 was always true
1309 file.seek(offset)
1310 blocksize = (
1311 min(count, constants.SENDFILE_FALLBACK_READBUFFER_SIZE)
1312 if count else constants.SENDFILE_FALLBACK_READBUFFER_SIZE
1313 )
1314 buf = bytearray(blocksize)
1315 total_sent = 0
1316 proto = _SendfileFallbackProtocol(transp)
1317 try:
1318 while True:
1319 if count:
1320 blocksize = min(count - total_sent, blocksize)
1321 if blocksize <= 0:
1322 return total_sent
1323 view = memoryview(buf)[:blocksize]
1324 read = await self.run_in_executor(None, file.readinto, view)
1325 if not read:
1326 return total_sent # EOF
1327 transp.write(view[:read])
1328 await proto.drain()
1329 total_sent += read
1330 finally:
1331 if total_sent > 0 and hasattr(file, 'seek'):
1332 file.seek(offset + total_sent)
1333 await proto.restore()
1335 async def start_tls(self, transport, protocol, sslcontext, *,
1336 server_side=False,
1337 server_hostname=None,
1338 ssl_handshake_timeout=None,
1339 ssl_shutdown_timeout=None):
1340 """Upgrade transport to TLS.
1342 Return a new transport that *protocol* should start using
1343 immediately.
1344 """
1345 if ssl is None: 1345 ↛ 1346line 1345 didn't jump to line 1346 because the condition on line 1345 was never true
1346 raise RuntimeError('Python ssl module is not available')
1348 if not isinstance(sslcontext, ssl.SSLContext):
1349 raise TypeError(
1350 f'sslcontext is expected to be an instance of ssl.SSLContext, '
1351 f'got {sslcontext!r}')
1353 if not getattr(transport, '_start_tls_compatible', False):
1354 raise TypeError(
1355 f'transport {transport!r} is not supported by start_tls()')
1357 waiter = self.create_future()
1358 ssl_protocol = sslproto.SSLProtocol(
1359 self, protocol, sslcontext, waiter,
1360 server_side, server_hostname,
1361 ssl_handshake_timeout=ssl_handshake_timeout,
1362 ssl_shutdown_timeout=ssl_shutdown_timeout,
1363 call_connection_made=False)
1365 # Pause early so that "ssl_protocol.data_received()" doesn't
1366 # have a chance to get called before "ssl_protocol.connection_made()".
1367 transport.pause_reading()
1369 # gh-142352: move buffered StreamReader data to SSLProtocol
1370 if server_side:
1371 from .streams import StreamReaderProtocol
1372 if isinstance(protocol, StreamReaderProtocol):
1373 stream_reader = getattr(protocol, '_stream_reader', None)
1374 if stream_reader is not None: 1374 ↛ 1380line 1374 didn't jump to line 1380 because the condition on line 1374 was always true
1375 buffer = stream_reader._buffer
1376 if buffer:
1377 ssl_protocol._incoming.write(buffer)
1378 buffer.clear()
1380 transport.set_protocol(ssl_protocol)
1381 conmade_cb = self.call_soon(ssl_protocol.connection_made, transport)
1382 resume_cb = self.call_soon(transport.resume_reading)
1384 try:
1385 await waiter
1386 except BaseException:
1387 transport.close()
1388 conmade_cb.cancel()
1389 resume_cb.cancel()
1390 waiter = None
1391 raise
1393 return ssl_protocol._app_transport
1395 async def create_datagram_endpoint(self, protocol_factory,
1396 local_addr=None, remote_addr=None, *,
1397 family=0, proto=0, flags=0,
1398 reuse_port=None,
1399 allow_broadcast=None, sock=None):
1400 """Create datagram connection."""
1401 if sock is not None:
1402 if sock.type == socket.SOCK_STREAM:
1403 raise ValueError(
1404 f'A datagram socket was expected, got {sock!r}')
1405 if (local_addr or remote_addr or
1406 family or proto or flags or
1407 reuse_port or allow_broadcast):
1408 # show the problematic kwargs in exception msg
1409 opts = dict(local_addr=local_addr, remote_addr=remote_addr,
1410 family=family, proto=proto, flags=flags,
1411 reuse_port=reuse_port,
1412 allow_broadcast=allow_broadcast)
1413 problems = ', '.join(f'{k}={v}' for k, v in opts.items() if v)
1414 raise ValueError(
1415 f'socket modifier keyword arguments can not be used '
1416 f'when sock is specified. ({problems})')
1417 sock.setblocking(False)
1418 r_addr = None
1419 else:
1420 if not (local_addr or remote_addr):
1421 if family == 0:
1422 raise ValueError('unexpected address family')
1423 addr_pairs_info = (((family, proto), (None, None)),)
1424 elif hasattr(socket, 'AF_UNIX') and family == socket.AF_UNIX:
1425 for addr in (local_addr, remote_addr):
1426 if addr is not None and not isinstance(addr, str): 1426 ↛ 1427line 1426 didn't jump to line 1427 because the condition on line 1426 was never true
1427 raise TypeError('string is expected')
1429 if local_addr and local_addr[0] not in (0, '\x00'): 1429 ↛ 1441line 1429 didn't jump to line 1441 because the condition on line 1429 was always true
1430 try:
1431 if stat.S_ISSOCK(os.stat(local_addr).st_mode): 1431 ↛ 1441line 1431 didn't jump to line 1441 because the condition on line 1431 was always true
1432 os.remove(local_addr)
1433 except FileNotFoundError:
1434 pass
1435 except OSError as err:
1436 # Directory may have permissions only to create socket.
1437 logger.error('Unable to check or remove stale UNIX '
1438 'socket %r: %r',
1439 local_addr, err)
1441 addr_pairs_info = (((family, proto),
1442 (local_addr, remote_addr)), )
1443 else:
1444 # join address by (family, protocol)
1445 addr_infos = {} # Using order preserving dict
1446 for idx, addr in ((0, local_addr), (1, remote_addr)):
1447 if addr is not None:
1448 if not (isinstance(addr, tuple) and len(addr) == 2):
1449 raise TypeError('2-tuple is expected')
1451 infos = await self._ensure_resolved(
1452 addr, family=family, type=socket.SOCK_DGRAM,
1453 proto=proto, flags=flags, loop=self)
1454 if not infos:
1455 raise OSError('getaddrinfo() returned empty list')
1457 for fam, _, pro, _, address in infos:
1458 key = (fam, pro)
1459 if key not in addr_infos: 1459 ↛ 1461line 1459 didn't jump to line 1461 because the condition on line 1459 was always true
1460 addr_infos[key] = [None, None]
1461 addr_infos[key][idx] = address
1463 # each addr has to have info for each (family, proto) pair
1464 addr_pairs_info = [
1465 (key, addr_pair) for key, addr_pair in addr_infos.items()
1466 if not ((local_addr and addr_pair[0] is None) or
1467 (remote_addr and addr_pair[1] is None))]
1469 if not addr_pairs_info:
1470 raise ValueError('can not get address information')
1472 exceptions = []
1474 for ((family, proto),
1475 (local_address, remote_address)) in addr_pairs_info:
1476 sock = None
1477 r_addr = None
1478 try:
1479 sock = socket.socket(
1480 family=family, type=socket.SOCK_DGRAM, proto=proto)
1481 if reuse_port:
1482 _set_reuseport(sock)
1483 if allow_broadcast:
1484 sock.setsockopt(
1485 socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
1486 sock.setblocking(False)
1488 if local_addr:
1489 sock.bind(local_address)
1490 if remote_addr:
1491 if not allow_broadcast:
1492 await self.sock_connect(sock, remote_address)
1493 r_addr = remote_address
1494 except OSError as exc:
1495 if sock is not None:
1496 sock.close()
1497 exceptions.append(exc)
1498 except:
1499 if sock is not None: 1499 ↛ 1501line 1499 didn't jump to line 1501 because the condition on line 1499 was always true
1500 sock.close()
1501 raise
1502 else:
1503 break
1504 else:
1505 raise exceptions[0]
1507 try:
1508 protocol = protocol_factory()
1509 except:
1510 # gh-156400: no transport owns the socket yet, so close it.
1511 sock.close()
1512 raise
1513 waiter = self.create_future()
1514 transport = self._make_datagram_transport(
1515 sock, protocol, r_addr, waiter)
1516 if self._debug: 1516 ↛ 1517line 1516 didn't jump to line 1517 because the condition on line 1516 was never true
1517 if local_addr:
1518 logger.info("Datagram endpoint local_addr=%r remote_addr=%r "
1519 "created: (%r, %r)",
1520 local_addr, remote_addr, transport, protocol)
1521 else:
1522 logger.debug("Datagram endpoint remote_addr=%r created: "
1523 "(%r, %r)",
1524 remote_addr, transport, protocol)
1526 try:
1527 await waiter
1528 except:
1529 transport.close()
1530 raise
1532 return transport, protocol
1534 async def _ensure_resolved(self, address, *,
1535 family=0, type=socket.SOCK_STREAM,
1536 proto=0, flags=0, loop):
1537 host, port = address[:2]
1538 info = _ipaddr_info(host, port, family, type, proto, *address[2:])
1539 if info is not None:
1540 # "host" is already a resolved IP.
1541 return [info]
1542 else:
1543 return await loop.getaddrinfo(host, port, family=family, type=type,
1544 proto=proto, flags=flags)
1546 async def _create_server_getaddrinfo(self, host, port, family, flags):
1547 infos = await self._ensure_resolved((host, port), family=family,
1548 type=socket.SOCK_STREAM,
1549 flags=flags, loop=self)
1550 if not infos:
1551 raise OSError(f'getaddrinfo({host!r}) returned empty list')
1552 return infos
1554 async def create_server(
1555 self, protocol_factory, host=None, port=None,
1556 *,
1557 family=socket.AF_UNSPEC,
1558 flags=socket.AI_PASSIVE,
1559 sock=None,
1560 backlog=100,
1561 ssl=None,
1562 reuse_address=None,
1563 reuse_port=None,
1564 keep_alive=None,
1565 ssl_handshake_timeout=None,
1566 ssl_shutdown_timeout=None,
1567 start_serving=True):
1568 """Create a TCP server.
1570 The host parameter can be a string, in that case the TCP server is
1571 bound to host and port.
1573 The host parameter can also be a sequence of strings and in that
1574 case the TCP server is bound to all hosts of the sequence. If
1575 a host appears multiple times (possibly indirectly e.g. when
1576 hostnames resolve to the same IP address), the server is only bound
1577 once to that host.
1579 Return a Server object which can be used to stop the service.
1581 This method is a coroutine.
1582 """
1583 if isinstance(ssl, bool): 1583 ↛ 1584line 1583 didn't jump to line 1584 because the condition on line 1583 was never true
1584 raise TypeError('ssl argument must be an SSLContext or None')
1586 if ssl_handshake_timeout is not None and ssl is None:
1587 raise ValueError(
1588 'ssl_handshake_timeout is only meaningful with ssl')
1590 if ssl_shutdown_timeout is not None and ssl is None: 1590 ↛ 1591line 1590 didn't jump to line 1591 because the condition on line 1590 was never true
1591 raise ValueError(
1592 'ssl_shutdown_timeout is only meaningful with ssl')
1594 if sock is not None:
1595 _check_ssl_socket(sock)
1597 if host is not None or port is not None:
1598 if sock is not None:
1599 raise ValueError(
1600 'host/port and sock can not be specified at the same time')
1602 if reuse_address is None: 1602 ↛ 1604line 1602 didn't jump to line 1604 because the condition on line 1602 was always true
1603 reuse_address = os.name == "posix" and sys.platform != "cygwin"
1604 sockets = []
1605 if host == '':
1606 hosts = [None]
1607 elif (isinstance(host, str) or
1608 not isinstance(host, collections.abc.Iterable)):
1609 hosts = [host]
1610 else:
1611 hosts = host
1613 fs = [self._create_server_getaddrinfo(host, port, family=family,
1614 flags=flags)
1615 for host in hosts]
1616 infos = await tasks.gather(*fs)
1617 infos = set(itertools.chain.from_iterable(infos))
1619 completed = False
1620 try:
1621 for res in infos:
1622 af, socktype, proto, canonname, sa = res
1623 try:
1624 sock = socket.socket(af, socktype, proto)
1625 except socket.error:
1626 # Assume it's a bad family/type/protocol combination.
1627 if self._debug:
1628 logger.warning('create_server() failed to create '
1629 'socket.socket(%r, %r, %r)',
1630 af, socktype, proto, exc_info=True)
1631 continue
1632 sockets.append(sock)
1633 if reuse_address: 1633 ↛ 1638line 1633 didn't jump to line 1638 because the condition on line 1633 was always true
1634 sock.setsockopt(
1635 socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
1636 # Since Linux 6.12.9, SO_REUSEPORT is not allowed
1637 # on other address families than AF_INET/AF_INET6.
1638 if reuse_port and af in (socket.AF_INET, socket.AF_INET6):
1639 _set_reuseport(sock)
1640 if keep_alive: 1640 ↛ 1641line 1640 didn't jump to line 1641 because the condition on line 1640 was never true
1641 sock.setsockopt(
1642 socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)
1643 # Disable IPv4/IPv6 dual stack support (enabled by
1644 # default on Linux) which makes a single socket
1645 # listen on both address families.
1646 if (_HAS_IPv6 and
1647 af == socket.AF_INET6 and
1648 hasattr(socket, 'IPPROTO_IPV6')):
1649 sock.setsockopt(socket.IPPROTO_IPV6,
1650 socket.IPV6_V6ONLY,
1651 True)
1652 try:
1653 sock.bind(sa)
1654 except OSError as err:
1655 msg = ('error while attempting '
1656 'to bind on address %r: %s'
1657 % (sa, str(err).lower()))
1658 if err.errno == errno.EADDRNOTAVAIL: 1658 ↛ 1660line 1658 didn't jump to line 1660 because the condition on line 1658 was never true
1659 # Assume the family is not enabled (bpo-30945)
1660 sockets.pop()
1661 sock.close()
1662 if self._debug:
1663 logger.warning(msg)
1664 continue
1665 raise OSError(err.errno, msg) from None
1667 if not sockets: 1667 ↛ 1668line 1667 didn't jump to line 1668 because the condition on line 1667 was never true
1668 raise OSError('could not bind on any address out of %r'
1669 % ([info[4] for info in infos],))
1671 completed = True
1672 finally:
1673 if not completed:
1674 for sock in sockets: 1674 ↛ 1683line 1674 didn't jump to line 1683 because the loop on line 1674 didn't complete
1675 sock.close()
1676 else:
1677 if sock is None:
1678 raise ValueError('Neither host/port nor sock were specified')
1679 if sock.type != socket.SOCK_STREAM:
1680 raise ValueError(f'A Stream Socket was expected, got {sock!r}')
1681 sockets = [sock]
1683 for sock in sockets:
1684 sock.setblocking(False)
1686 server = Server(self, sockets, protocol_factory,
1687 ssl, backlog, ssl_handshake_timeout,
1688 ssl_shutdown_timeout)
1689 if start_serving:
1690 server._start_serving()
1691 # Skip one loop iteration so that all 'loop.add_reader'
1692 # go through.
1693 await tasks.sleep(0)
1695 if self._debug:
1696 logger.info("%r is serving", server)
1697 return server
1699 async def connect_accepted_socket(
1700 self, protocol_factory, sock,
1701 *, ssl=None,
1702 ssl_handshake_timeout=None,
1703 ssl_shutdown_timeout=None):
1704 if sock.type != socket.SOCK_STREAM: 1704 ↛ 1705line 1704 didn't jump to line 1705 because the condition on line 1704 was never true
1705 raise ValueError(f'A Stream Socket was expected, got {sock!r}')
1707 if ssl_handshake_timeout is not None and not ssl:
1708 raise ValueError(
1709 'ssl_handshake_timeout is only meaningful with ssl')
1711 if ssl_shutdown_timeout is not None and not ssl: 1711 ↛ 1712line 1711 didn't jump to line 1712 because the condition on line 1711 was never true
1712 raise ValueError(
1713 'ssl_shutdown_timeout is only meaningful with ssl')
1715 _check_ssl_socket(sock)
1717 transport, protocol = await self._create_connection_transport(
1718 sock, protocol_factory, ssl, '', server_side=True,
1719 ssl_handshake_timeout=ssl_handshake_timeout,
1720 ssl_shutdown_timeout=ssl_shutdown_timeout)
1721 if self._debug: 1721 ↛ 1724line 1721 didn't jump to line 1724 because the condition on line 1721 was never true
1722 # Get the socket from the transport because SSL transport closes
1723 # the old socket and creates a new SSL socket
1724 sock = transport.get_extra_info('socket')
1725 logger.debug("%r handled: (%r, %r)", sock, transport, protocol)
1726 return transport, protocol
1728 async def connect_read_pipe(self, protocol_factory, pipe):
1729 try:
1730 protocol = protocol_factory()
1731 except:
1732 # gh-156400: no transport owns the pipe yet, so close it.
1733 pipe.close()
1734 raise
1735 waiter = self.create_future()
1736 transport = self._make_read_pipe_transport(pipe, protocol, waiter)
1738 try:
1739 await waiter
1740 except:
1741 transport.close()
1742 raise
1744 if self._debug: 1744 ↛ 1745line 1744 didn't jump to line 1745 because the condition on line 1744 was never true
1745 logger.debug('Read pipe %r connected: (%r, %r)',
1746 pipe.fileno(), transport, protocol)
1747 return transport, protocol
1749 async def connect_write_pipe(self, protocol_factory, pipe):
1750 try:
1751 protocol = protocol_factory()
1752 except:
1753 # gh-156400: no transport owns the pipe yet, so close it.
1754 pipe.close()
1755 raise
1756 waiter = self.create_future()
1757 transport = self._make_write_pipe_transport(pipe, protocol, waiter)
1759 try:
1760 await waiter
1761 except:
1762 transport.close()
1763 raise
1765 if self._debug: 1765 ↛ 1766line 1765 didn't jump to line 1766 because the condition on line 1765 was never true
1766 logger.debug('Write pipe %r connected: (%r, %r)',
1767 pipe.fileno(), transport, protocol)
1768 return transport, protocol
1770 def _log_subprocess(self, msg, stdin, stdout, stderr):
1771 info = [msg]
1772 if stdin is not None:
1773 info.append(f'stdin={_format_pipe(stdin)}')
1774 if stdout is not None and stderr == subprocess.STDOUT:
1775 info.append(f'stdout=stderr={_format_pipe(stdout)}')
1776 else:
1777 if stdout is not None:
1778 info.append(f'stdout={_format_pipe(stdout)}')
1779 if stderr is not None:
1780 info.append(f'stderr={_format_pipe(stderr)}')
1781 logger.debug(' '.join(info))
1783 async def subprocess_shell(self, protocol_factory, cmd, *,
1784 stdin=subprocess.PIPE,
1785 stdout=subprocess.PIPE,
1786 stderr=subprocess.PIPE,
1787 universal_newlines=False,
1788 shell=True, bufsize=0,
1789 encoding=None, errors=None, text=None,
1790 **kwargs):
1791 if not isinstance(cmd, (bytes, str)):
1792 raise ValueError("cmd must be a string")
1793 if universal_newlines:
1794 raise ValueError("universal_newlines must be False")
1795 if not shell:
1796 raise ValueError("shell must be True")
1797 if bufsize != 0:
1798 raise ValueError("bufsize must be 0")
1799 if text:
1800 raise ValueError("text must be False")
1801 if encoding is not None:
1802 raise ValueError("encoding must be None")
1803 if errors is not None:
1804 raise ValueError("errors must be None")
1806 protocol = protocol_factory()
1807 debug_log = None
1808 if self._debug: 1808 ↛ 1811line 1808 didn't jump to line 1811 because the condition on line 1808 was never true
1809 # don't log parameters: they may contain sensitive information
1810 # (password) and may be too long
1811 debug_log = 'run shell command %r' % cmd
1812 self._log_subprocess(debug_log, stdin, stdout, stderr)
1813 transport = await self._make_subprocess_transport(
1814 protocol, cmd, True, stdin, stdout, stderr, bufsize, **kwargs)
1815 if self._debug and debug_log is not None: 1815 ↛ 1816line 1815 didn't jump to line 1816 because the condition on line 1815 was never true
1816 logger.info('%s: %r', debug_log, transport)
1817 return transport, protocol
1819 async def subprocess_exec(self, protocol_factory, program, *args,
1820 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
1821 stderr=subprocess.PIPE, universal_newlines=False,
1822 shell=False, bufsize=0,
1823 encoding=None, errors=None, text=None,
1824 **kwargs):
1825 if universal_newlines:
1826 raise ValueError("universal_newlines must be False")
1827 if shell:
1828 raise ValueError("shell must be False")
1829 if bufsize != 0:
1830 raise ValueError("bufsize must be 0")
1831 if text:
1832 raise ValueError("text must be False")
1833 if encoding is not None:
1834 raise ValueError("encoding must be None")
1835 if errors is not None:
1836 raise ValueError("errors must be None")
1838 popen_args = (program,) + args
1839 protocol = protocol_factory()
1840 debug_log = None
1841 if self._debug: 1841 ↛ 1844line 1841 didn't jump to line 1844 because the condition on line 1841 was never true
1842 # don't log parameters: they may contain sensitive information
1843 # (password) and may be too long
1844 debug_log = f'execute program {program!r}'
1845 self._log_subprocess(debug_log, stdin, stdout, stderr)
1846 transport = await self._make_subprocess_transport(
1847 protocol, popen_args, False, stdin, stdout, stderr,
1848 bufsize, **kwargs)
1849 if self._debug and debug_log is not None: 1849 ↛ 1850line 1849 didn't jump to line 1850 because the condition on line 1849 was never true
1850 logger.info('%s: %r', debug_log, transport)
1851 return transport, protocol
1853 def get_exception_handler(self):
1854 """Return an exception handler, or None if the default one is in use.
1855 """
1856 return self._exception_handler
1858 def set_exception_handler(self, handler):
1859 """Set handler as the new event loop exception handler.
1861 If handler is None, the default exception handler will
1862 be set.
1864 If handler is a callable object, it should have a
1865 signature matching '(loop, context)', where 'loop'
1866 will be a reference to the active event loop, 'context'
1867 will be a dict object (see `call_exception_handler()`
1868 documentation for details about context).
1869 """
1870 if handler is not None and not callable(handler):
1871 raise TypeError(f'A callable object or None is expected, '
1872 f'got {handler!r}')
1873 self._exception_handler = handler
1875 def default_exception_handler(self, context):
1876 """Default exception handler.
1878 This is called when an exception occurs and no exception
1879 handler is set, and can be called by a custom exception
1880 handler that wants to defer to the default behavior.
1882 This default handler logs the error message and other
1883 context-dependent information. In debug mode, a truncated
1884 stack trace is also appended showing where the given object
1885 (e.g. a handle or future or task) was created, if any.
1887 The context parameter has the same meaning as in
1888 `call_exception_handler()`.
1889 """
1890 message = context.get('message')
1891 if not message: 1891 ↛ 1892line 1891 didn't jump to line 1892 because the condition on line 1891 was never true
1892 message = 'Unhandled exception in event loop'
1894 exception = context.get('exception')
1895 if exception is not None:
1896 exc_info = (type(exception), exception, exception.__traceback__)
1897 else:
1898 exc_info = False
1900 if ('source_traceback' not in context and
1901 self._current_handle is not None and
1902 self._current_handle._source_traceback):
1903 context['handle_traceback'] = \
1904 self._current_handle._source_traceback
1906 log_lines = [message]
1907 for key in sorted(context):
1908 if key in {'message', 'exception'}:
1909 continue
1910 value = context[key]
1911 if key == 'source_traceback':
1912 tb = ''.join(traceback.format_list(value))
1913 value = 'Object created at (most recent call last):\n'
1914 value += tb.rstrip()
1915 elif key == 'handle_traceback':
1916 tb = ''.join(traceback.format_list(value))
1917 value = 'Handle created at (most recent call last):\n'
1918 value += tb.rstrip()
1919 else:
1920 value = repr(value)
1921 log_lines.append(f'{key}: {value}')
1923 logger.error('\n'.join(log_lines), exc_info=exc_info)
1925 def call_exception_handler(self, context):
1926 """Call the current event loop's exception handler.
1928 The context argument is a dict containing the following keys:
1930 - 'message': Error message;
1931 - 'exception' (optional): Exception object;
1932 - 'future' (optional): Future instance;
1933 - 'task' (optional): Task instance;
1934 - 'handle' (optional): Handle instance;
1935 - 'protocol' (optional): Protocol instance;
1936 - 'transport' (optional): Transport instance;
1937 - 'socket' (optional): Socket instance;
1938 - 'source_traceback' (optional): Traceback of the source;
1939 - 'handle_traceback' (optional): Traceback of the handle;
1940 - 'asyncgen' (optional): Asynchronous generator that caused
1941 the exception.
1943 New keys maybe introduced in the future.
1945 Note: do not overload this method in an event loop subclass.
1946 For custom exception handling, use the
1947 `set_exception_handler()` method.
1948 """
1949 if self._exception_handler is None:
1950 try:
1951 self.default_exception_handler(context)
1952 except (SystemExit, KeyboardInterrupt):
1953 raise
1954 except BaseException:
1955 # Second protection layer for unexpected errors
1956 # in the default implementation, as well as for subclassed
1957 # event loops with overloaded "default_exception_handler".
1958 logger.error('Exception in default exception handler',
1959 exc_info=True)
1960 else:
1961 try:
1962 ctx = None
1963 thing = context.get("task")
1964 if thing is None:
1965 # Even though Futures don't have a context,
1966 # Task is a subclass of Future,
1967 # and sometimes the 'future' key holds a Task.
1968 thing = context.get("future")
1969 if thing is None:
1970 # Handles also have a context.
1971 thing = context.get("handle")
1972 if thing is not None and hasattr(thing, "get_context"):
1973 ctx = thing.get_context()
1974 if ctx is not None and hasattr(ctx, "run"):
1975 ctx.run(self._exception_handler, self, context)
1976 else:
1977 self._exception_handler(self, context)
1978 except (SystemExit, KeyboardInterrupt):
1979 raise
1980 except BaseException as exc:
1981 # Exception in the user set custom exception handler.
1982 try:
1983 # Let's try default handler.
1984 self.default_exception_handler({
1985 'message': 'Unhandled error in exception handler',
1986 'exception': exc,
1987 'context': context,
1988 })
1989 except (SystemExit, KeyboardInterrupt):
1990 raise
1991 except BaseException:
1992 # Guard 'default_exception_handler' in case it is
1993 # overloaded.
1994 logger.error('Exception in default exception handler '
1995 'while handling an unexpected error '
1996 'in custom exception handler',
1997 exc_info=True)
1999 def _add_callback(self, handle):
2000 """Add a Handle to _ready."""
2001 if not handle._cancelled:
2002 self._ready.append(handle)
2004 def _add_callback_signalsafe(self, handle):
2005 """Like _add_callback() but called from a signal handler."""
2006 self._add_callback(handle)
2007 self._write_to_self()
2009 def _timer_handle_cancelled(self, handle):
2010 """Notification that a TimerHandle has been cancelled."""
2011 if handle._scheduled:
2012 self._timer_cancelled_count += 1
2014 def _run_once(self):
2015 """Run one full iteration of the event loop.
2017 This calls all currently ready callbacks, polls for I/O,
2018 schedules the resulting callbacks, and finally schedules
2019 'call_later' callbacks.
2020 """
2022 sched_count = len(self._scheduled)
2023 if (sched_count > _MIN_SCHEDULED_TIMER_HANDLES and
2024 self._timer_cancelled_count / sched_count >
2025 _MIN_CANCELLED_TIMER_HANDLES_FRACTION):
2026 # Remove delayed calls that were cancelled if their number
2027 # is too high
2028 new_scheduled = []
2029 for handle in self._scheduled:
2030 if handle._cancelled:
2031 handle._scheduled = False
2032 else:
2033 new_scheduled.append(handle)
2035 heapq.heapify(new_scheduled)
2036 self._scheduled = new_scheduled
2037 self._timer_cancelled_count = 0
2038 else:
2039 # Remove delayed calls that were cancelled from head of queue.
2040 while self._scheduled and self._scheduled[0]._cancelled:
2041 self._timer_cancelled_count -= 1
2042 handle = heapq.heappop(self._scheduled)
2043 handle._scheduled = False
2045 timeout = None
2046 if self._ready or self._stopping:
2047 timeout = 0
2048 elif self._scheduled:
2049 # Compute the desired timeout.
2050 timeout = self._scheduled[0]._when - self.time()
2051 if timeout > MAXIMUM_SELECT_TIMEOUT: 2051 ↛ 2052line 2051 didn't jump to line 2052 because the condition on line 2051 was never true
2052 timeout = MAXIMUM_SELECT_TIMEOUT
2053 elif timeout < 0:
2054 timeout = 0
2056 event_list = self._selector.select(timeout)
2057 self._process_events(event_list)
2058 # Needed to break cycles when an exception occurs.
2059 event_list = None
2061 # Handle 'later' callbacks that are ready.
2062 now = self.time()
2063 # Ensure that `end_time` is strictly increasing
2064 # when the clock resolution is too small.
2065 end_time = now + max(constants.CLOCK_RESOLUTION, math.ulp(now))
2066 while self._scheduled:
2067 handle = self._scheduled[0]
2068 if handle._when >= end_time:
2069 break
2070 handle = heapq.heappop(self._scheduled)
2071 handle._scheduled = False
2072 self._ready.append(handle)
2074 # This is the only place where callbacks are actually *called*.
2075 # All other places just add them to ready.
2076 # Note: We run all currently scheduled callbacks, but not any
2077 # callbacks scheduled by callbacks run this time around --
2078 # they will be run the next time (after another I/O poll).
2079 # Use an idiom that is thread-safe without using locks.
2080 ntodo = len(self._ready)
2081 for i in range(ntodo):
2082 handle = self._ready.popleft()
2083 if handle._cancelled:
2084 continue
2085 if self._debug:
2086 try:
2087 self._current_handle = handle
2088 t0 = self.time()
2089 handle._run()
2090 dt = self.time() - t0
2091 if dt >= self.slow_callback_duration:
2092 logger.warning('Executing %s took %.3f seconds',
2093 _format_handle(handle), dt)
2094 finally:
2095 self._current_handle = None
2096 else:
2097 handle._run()
2098 handle = None # Needed to break cycles when an exception occurs.
2100 def _set_coroutine_origin_tracking(self, enabled):
2101 if bool(enabled) == bool(self._coroutine_origin_tracking_enabled):
2102 return
2104 if enabled:
2105 self._coroutine_origin_tracking_saved_depth = (
2106 sys.get_coroutine_origin_tracking_depth())
2107 sys.set_coroutine_origin_tracking_depth(
2108 constants.DEBUG_STACK_DEPTH)
2109 else:
2110 sys.set_coroutine_origin_tracking_depth(
2111 self._coroutine_origin_tracking_saved_depth)
2113 self._coroutine_origin_tracking_enabled = enabled
2115 def get_debug(self):
2116 return self._debug
2118 def set_debug(self, enabled):
2119 self._debug = enabled
2121 if self.is_running():
2122 self.call_soon_threadsafe(self._set_coroutine_origin_tracking, enabled)