18.5.1.Base Event Loop¶
Source code:Lib/asyncio/events.py
The event loop is the central execution device provided byasyncio.It provides multiple facilities, including:
Registering, executing and cancelling delayed calls (timeouts).
Creating client and servertransports for variouskinds of communication.
Launching subprocesses and the associatedtransports for communication with an external program.
Delegating costly function calls to a pool of threads.
- class
asyncio.BaseEventLoop¶ This class is an implementation detail. It is a subclass of
AbstractEventLoopand may be a base class of concreteevent loop implementations found inasyncio. It should notbe used directly; useAbstractEventLoopinstead.BaseEventLoopshould not be subclassed by third-party code; theinternal interface is not stable.
- class
asyncio.AbstractEventLoop¶ Abstract base class of event loops.
This class isnot thread safe.
18.5.1.1.Run an event loop¶
AbstractEventLoop.run_forever()¶Run until
stop()is called. Ifstop()is called beforerun_forever()is called, this polls the I/O selector oncewith a timeout of zero, runs all callbacks scheduled in response toI/O events (and those that were already scheduled), and then exits.Ifstop()is called whilerun_forever()is running,this will run the current batch of callbacks and then exit. Notethat callbacks scheduled by callbacks will not run in that case;they will run the next timerun_forever()is called.Changed in version 3.5.1.
AbstractEventLoop.run_until_complete(future)¶Run until the
Futureis done.If the argument is acoroutine object, it is wrapped by
ensure_future().Return the Future’s result, or raise its exception.
AbstractEventLoop.is_running()¶Returns running status of event loop.
AbstractEventLoop.stop()¶Stop running the event loop.
This causes
run_forever()to exit at the next suitableopportunity (see there for more details).Changed in version 3.5.1.
AbstractEventLoop.is_closed()¶Returns
Trueif the event loop was closed.New in version 3.4.2.
AbstractEventLoop.close()¶Close the event loop. The loop must not be running. Pendingcallbacks will be lost.
This clears the queues and shuts down the executor, but does not wait forthe executor to finish.
This is idempotent and irreversible. No other methods should be called afterthis one.
- coroutine
AbstractEventLoop.shutdown_asyncgens()¶ Schedule all currently openasynchronous generator objects toclose with an
aclose()call. After calling this method,the event loop will issue a warning whenever a new asynchronous generatoris iterated. Should be used to finalize all scheduled asynchronousgenerators reliably. Example:try:loop.run_forever()finally:loop.run_until_complete(loop.shutdown_asyncgens())loop.close()
New in version 3.6.
18.5.1.2.Calls¶
Mostasyncio functions don’t accept keywords. If you want to passkeywords to your callback, usefunctools.partial(). For example,loop.call_soon(functools.partial(print,"Hello",flush=True)) will callprint("Hello",flush=True).
Note
functools.partial() is better thanlambda functions, becauseasyncio can inspectfunctools.partial() object to displayparameters in debug mode, whereaslambda functions have a poorrepresentation.
AbstractEventLoop.call_soon(callback,*args)¶Arrange for a callback to be called as soon as possible. The callback iscalled after
call_soon()returns, when control returns to the eventloop.This operates as aFIFO queue, callbacksare called in the order in which they are registered. Each callbackwill be called exactly once.
Any positional arguments after the callback will be passed to thecallback when it is called.
An instance of
asyncio.Handleis returned, which can beused to cancel the callback.
AbstractEventLoop.call_soon_threadsafe(callback,*args)¶Like
call_soon(), but thread safe.See theconcurrency and multithreadingsection of the documentation.
18.5.1.3.Delayed calls¶
The event loop has its own internal clock for computing timeouts.Which clock is used depends on the (platform-specific) event loopimplementation; ideally it is a monotonic clock. This will generally bea different clock thantime.time().
Note
Timeouts (relativedelay or absolutewhen) should not exceed one day.
AbstractEventLoop.call_later(delay,callback,*args)¶Arrange for thecallback to be called after the givendelayseconds (either an int or float).
An instance of
asyncio.Handleis returned, which can beused to cancel the callback.callback will be called exactly once per call to
call_later().If two callbacks are scheduled for exactly the same time, it isundefined which will be called first.The optional positionalargs will be passed to the callback when itis called. If you want the callback to be called with some namedarguments, use a closure or
functools.partial().
AbstractEventLoop.call_at(when,callback,*args)¶Arrange for thecallback to be called at the given absolute timestampwhen (an int or float), using the same time reference as
AbstractEventLoop.time().This method’s behavior is the same as
call_later().An instance of
asyncio.Handleis returned, which can beused to cancel the callback.
AbstractEventLoop.time()¶Return the current time, as a
floatvalue, according to theevent loop’s internal clock.
See also
Theasyncio.sleep() function.
18.5.1.4.Futures¶
AbstractEventLoop.create_future()¶Create an
asyncio.Futureobject attached to the loop.This is a preferred way to create futures in asyncio, as eventloop implementations can provide alternative implementationsof the Future class (with better performance or instrumentation).
New in version 3.5.2.
18.5.1.5.Tasks¶
AbstractEventLoop.create_task(coro)¶Schedule the execution of acoroutine object: wrap it ina future. Return a
Taskobject.Third-party event loops can use their own subclass of
Taskforinteroperability. In this case, the result type is a subclass ofTask.This method was added in Python 3.4.2. Use the
async()function tosupport also older Python versions.New in version 3.4.2.
AbstractEventLoop.set_task_factory(factory)¶Set a task factory that will be used by
AbstractEventLoop.create_task().Iffactory is
Nonethe default task factory will be set.Iffactory is acallable, it should have a signature matching
(loop,coro), whereloop will be a reference to the activeevent loop,coro will be a coroutine object. The callablemust return anasyncio.Futurecompatible object.New in version 3.4.4.
AbstractEventLoop.get_task_factory()¶Return a task factory, or
Noneif the default one is in use.New in version 3.4.4.
18.5.1.6.Creating connections¶
- coroutine
AbstractEventLoop.create_connection(protocol_factory,host=None,port=None,*,ssl=None,family=0,proto=0,flags=0,sock=None,local_addr=None,server_hostname=None)¶ Create a streaming transport connection to a given Internethost andport: socket family
AF_INETorAF_INET6depending onhost (orfamily if specified),socket typeSOCK_STREAM.protocol_factory must be acallable returning aprotocol instance.This method is acoroutine which will try toestablish the connection in the background. When successful, thecoroutine returns a
(transport,protocol)pair.The chronological synopsis of the underlying operation is as follows:
The connection is established, and atransportis created to represent it.
protocol_factory is called without arguments and must return aprotocol instance.
The protocol instance is tied to the transport, and its
connection_made()method is called.The coroutine returns successfully with the
(transport,protocol)pair.
The created transport is an implementation-dependent bidirectional stream.
Note
protocol_factory can be any kind of callable, not necessarilya class. For example, if you want to use a pre-createdprotocol instance, you can pass
lambda:my_protocol.Options that change how the connection is created:
ssl: if given and not false, a SSL/TLS transport is created(by default a plain TCP transport is created). Ifssl isa
ssl.SSLContextobject, this context is used to createthe transport; ifssl isTrue, a context with someunspecified default settings is used.See also
server_hostname, is only for use together withssl,and sets or overrides the hostname that the target server’s certificatewill be matched against. By default the value of thehost argumentis used. Ifhost is empty, there is no default and you must pass avalue forserver_hostname. Ifserver_hostname is an emptystring, hostname matching is disabled (which is a serious securityrisk, allowing for man-in-the-middle-attacks).
family,proto,flags are the optional address family, protocoland flags to be passed through to getaddrinfo() forhost resolution.If given, these should all be integers from the corresponding
socketmodule constants.sock, if given, should be an existing, already connected
socket.socketobject to be used by the transport.Ifsock is given, none ofhost,port,family,proto,flagsandlocal_addr should be specified.local_addr, if given, is a
(local_host,local_port)tuple usedto bind the socket to locally. Thelocal_host andlocal_portare looked up using getaddrinfo(), similarly tohost andport.
Changed in version 3.5:On Windows with
ProactorEventLoop, SSL/TLS is now supported.See also
The
open_connection()function can be used to get a pair of(StreamReader,StreamWriter) instead of a protocol.
- coroutine
AbstractEventLoop.create_datagram_endpoint(protocol_factory,local_addr=None,remote_addr=None,*,family=0,proto=0,flags=0,reuse_address=None,reuse_port=None,allow_broadcast=None,sock=None)¶ Note
The parameterreuse_address is no longer supported, as using
SO_REUSEADDRposes a significant security concern forUDP. Explicitly passingreuse_address=Truewill raise an exception.When multiple processes with differing UIDs assign sockets to anindentical UDP socket address with
SO_REUSEADDR, incoming packets canbecome randomly distributed among the sockets.For supported platforms,reuse_port can be used as a replacement forsimilar functionality. Withreuse_port,
SO_REUSEPORTis used instead, which specificallyprevents processes with differing UIDs from assigning sockets to the samesocket address.Create a datagram connection.
Create datagram connection: socket family
AF_INETorAF_INET6depending onhost (orfamily if specified),socket typeSOCK_DGRAM.protocol_factory must be acallable returning aprotocol instance.This method is acoroutine which will try toestablish the connection in the background. When successful, thecoroutine returns a
(transport,protocol)pair.Options changing how the connection is created:
local_addr, if given, is a
(local_host,local_port)tuple usedto bind the socket to locally. Thelocal_host andlocal_portare looked up usinggetaddrinfo().remote_addr, if given, is a
(remote_host,remote_port)tuple usedto connect the socket to a remote address. Theremote_host andremote_port are looked up usinggetaddrinfo().family,proto,flags are the optional address family, protocoland flags to be passed through to
getaddrinfo()forhostresolution. If given, these should all be integers from thecorrespondingsocketmodule constants.reuse_port tells the kernel to allow this endpoint to be bound to thesame port as other existing endpoints are bound to, so long as they allset this flag when being created. This option is not supported on Windowsand some UNIX’s. If the
SO_REUSEPORTconstant is notdefined then this capability is unsupported.allow_broadcast tells the kernel to allow this endpoint to sendmessages to the broadcast address.
sock can optionally be specified in order to use a preexisting,already connected,
socket.socketobject to be used by thetransport. If specified,local_addr andremote_addr should be omitted(must beNone).
On Windows with
ProactorEventLoop, this method is not supported.SeeUDP echo client protocol andUDP echo server protocol examples.
Changed in version 3.4.4:Thefamily,proto,flags,reuse_address,reuse_port,*allow_broadcast, andsock parameters were added.
Changed in version 3.6.10:Thereuse_address parameter is no longer supporter due to securityconcerns
- coroutine
AbstractEventLoop.create_unix_connection(protocol_factory,path,*,ssl=None,sock=None,server_hostname=None)¶ Create UNIX connection: socket family
AF_UNIX, sockettypeSOCK_STREAM. TheAF_UNIXsocketfamily is used to communicate between processes on the same machineefficiently.This method is acoroutine which will try toestablish the connection in the background. When successful, thecoroutine returns a
(transport,protocol)pair.path is the name of a UNIX domain socket, and is required unless asockparameter is specified. Abstract UNIX sockets,
str, andbytespaths are supported.See the
AbstractEventLoop.create_connection()method for parameters.Availability: UNIX.
18.5.1.7.Creating listening connections¶
- coroutine
AbstractEventLoop.create_server(protocol_factory,host=None,port=None,*,family=socket.AF_UNSPEC,flags=socket.AI_PASSIVE,sock=None,backlog=100,ssl=None,reuse_address=None,reuse_port=None)¶ Create a TCP server (socket type
SOCK_STREAM) bound tohost andport.Return a
Serverobject, itssocketsattributecontains created sockets. Use theServer.close()method to stop theserver: close listening sockets.Parameters:
Thehost parameter can be a string, in that case the TCP server isbound tohost andport. Thehost parameter can also be a sequenceof strings and in that case the TCP server is bound to all hosts of thesequence. Ifhost is an empty string or
None, all interfaces areassumed and a list of multiple sockets will be returned (most likely onefor IPv4 and another one for IPv6).family can be set to either
socket.AF_INETorAF_INET6to force the socket to use IPv4 or IPv6. If not setit will be determined from host (defaults tosocket.AF_UNSPEC).flags is a bitmask for
getaddrinfo().sock can optionally be specified in order to use a preexistingsocket object. If specified,host andport should be omitted (must be
None).backlog is the maximum number of queued connections passed to
listen()(defaults to 100).ssl can be set to an
SSLContextto enable SSL over theaccepted connections.reuse_address tells the kernel to reuse a local socket inTIME_WAIT state, without waiting for its natural timeout toexpire. If not specified will automatically be set to
TrueonUNIX.reuse_port tells the kernel to allow this endpoint to be bound to thesame port as other existing endpoints are bound to, so long as they allset this flag when being created. This option is not supported onWindows.
This method is acoroutine.
Changed in version 3.5:On Windows with
ProactorEventLoop, SSL/TLS is now supported.See also
The function
start_server()creates a (StreamReader,StreamWriter) pair and calls back a function with this pair.Changed in version 3.5.1:Thehost parameter can now be a sequence of strings.
- coroutine
AbstractEventLoop.create_unix_server(protocol_factory,path=None,*,sock=None,backlog=100,ssl=None)¶ Similar to
AbstractEventLoop.create_server(), but specific to thesocket familyAF_UNIX.This method is acoroutine.
Availability: UNIX.
- coroutine
BaseEventLoop.connect_accepted_socket(protocol_factory,sock,*,ssl=None)¶ Handle an accepted connection.
This is used by servers that accept connections outside ofasyncio but that use asyncio to handle them.
Parameters:
sock is a preexisting socket object returned from an
acceptcall.ssl can be set to an
SSLContextto enable SSL over theaccepted connections.
This method is acoroutine. When completed, thecoroutine returns a
(transport,protocol)pair.New in version 3.5.3.
18.5.1.8.Watch file descriptors¶
On Windows withSelectorEventLoop, only socket handles are supported(ex: pipe file descriptors are not supported).
On Windows withProactorEventLoop, these methods are not supported.
AbstractEventLoop.add_reader(fd,callback,*args)¶Start watching the file descriptor for read availability and then call thecallback with specified arguments.
AbstractEventLoop.remove_reader(fd)¶Stop watching the file descriptor for read availability.
AbstractEventLoop.add_writer(fd,callback,*args)¶Start watching the file descriptor for write availability and then call thecallback with specified arguments.
AbstractEventLoop.remove_writer(fd)¶Stop watching the file descriptor for write availability.
Thewatch a file descriptor for read eventsexample uses the low-levelAbstractEventLoop.add_reader() method to registerthe file descriptor of a socket.
18.5.1.9.Low-level socket operations¶
- coroutine
AbstractEventLoop.sock_recv(sock,nbytes)¶ Receive data from the socket. Modeled after blocking
socket.socket.recv()method.The return value is a bytes objectrepresenting the data received. The maximum amount of data to be receivedat once is specified bynbytes.
With
SelectorEventLoopevent loop, the socketsock must benon-blocking.This method is acoroutine.
- coroutine
AbstractEventLoop.sock_sendall(sock,data)¶ Send data to the socket. Modeled after blocking
socket.socket.sendall()method.The socket must be connected to a remote socket.This method continues to send data fromdata until either all data hasbeen sent or an error occurs.
Noneis returned on success. On error,an exception is raised, and there is no way to determine how much data, ifany, was successfully processed by the receiving end of the connection.With
SelectorEventLoopevent loop, the socketsock must benon-blocking.This method is acoroutine.
- coroutine
AbstractEventLoop.sock_connect(sock,address)¶ Connect to a remote socket ataddress. Modeled afterblocking
socket.socket.connect()method.With
SelectorEventLoopevent loop, the socketsock must benon-blocking.This method is acoroutine.
Changed in version 3.5.2:
addressno longer needs to be resolved.sock_connectwill try to check if theaddress is already resolved by callingsocket.inet_pton(). If not,AbstractEventLoop.getaddrinfo()will be used to resolve theaddress.
- coroutine
AbstractEventLoop.sock_accept(sock)¶ Accept a connection. Modeled after blocking
socket.socket.accept().The socket must be bound to an address and listeningfor connections. The return value is a pair
(conn,address)whereconnis anew socket object usable to send and receive data on the connection,andaddress is the address bound to the socket on the other end of theconnection.The socketsock must be non-blocking.
This method is acoroutine.
See also
18.5.1.10.Resolve host name¶
- coroutine
AbstractEventLoop.getaddrinfo(host,port,*,family=0,type=0,proto=0,flags=0)¶ This method is acoroutine, similar to
socket.getaddrinfo()function but non-blocking.
- coroutine
AbstractEventLoop.getnameinfo(sockaddr,flags=0)¶ This method is acoroutine, similar to
socket.getnameinfo()function but non-blocking.
18.5.1.11.Connect pipes¶
On Windows withSelectorEventLoop, these methods are not supported.UseProactorEventLoop to support pipes on Windows.
- coroutine
AbstractEventLoop.connect_read_pipe(protocol_factory,pipe)¶ Register read pipe in eventloop.
protocol_factory should instantiate object with
Protocolinterface.pipe is afile-like object.Return pair(transport,protocol), wheretransport supports theReadTransportinterface.With
SelectorEventLoopevent loop, thepipe is set tonon-blocking mode.This method is acoroutine.
- coroutine
AbstractEventLoop.connect_write_pipe(protocol_factory,pipe)¶ Register write pipe in eventloop.
protocol_factory should instantiate object with
BaseProtocolinterface.pipe isfile-like object.Return pair(transport,protocol), wheretransport supportsWriteTransportinterface.With
SelectorEventLoopevent loop, thepipe is set tonon-blocking mode.This method is acoroutine.
See also
TheAbstractEventLoop.subprocess_exec() andAbstractEventLoop.subprocess_shell() methods.
18.5.1.12.UNIX signals¶
Availability: UNIX only.
AbstractEventLoop.add_signal_handler(signum,callback,*args)¶Add a handler for a signal.
Raise
ValueErrorif the signal number is invalid or uncatchable.RaiseRuntimeErrorif there is a problem setting up the handler.
AbstractEventLoop.remove_signal_handler(sig)¶Remove a handler for a signal.
Return
Trueif a signal handler was removed,Falseif not.
See also
Thesignal module.
18.5.1.13.Executor¶
Call a function in anExecutor (pool of threads orpool of processes). By default, an event loop uses a thread pool executor(ThreadPoolExecutor).
- coroutine
AbstractEventLoop.run_in_executor(executor,func,*args)¶ Arrange for afunc to be called in the specified executor.
Theexecutor argument should be an
Executorinstance. The default executor is used ifexecutor isNone.Use functools.partial to pass keywords to the *func*.
This method is acoroutine.
Changed in version 3.5.3:
BaseEventLoop.run_in_executor()no longer configures themax_workersof the thread pool executor it creates, insteadleaving it up to the thread pool executor(ThreadPoolExecutor) to set thedefault.
AbstractEventLoop.set_default_executor(executor)¶Set the default executor used by
run_in_executor().
18.5.1.14.Error Handling API¶
Allows customizing how exceptions are handled in the event loop.
AbstractEventLoop.set_exception_handler(handler)¶Sethandler as the new event loop exception handler.
Ifhandler is
None, the default exception handler willbe set.Ifhandler is a callable object, it should have amatching signature to
(loop,context), whereloopwill be a reference to the active event loop,contextwill be adictobject (seecall_exception_handler()documentation for details about context).
AbstractEventLoop.get_exception_handler()¶Return the exception handler, or
Noneif the default oneis in use.New in version 3.5.2.
AbstractEventLoop.default_exception_handler(context)¶Default exception handler.
This is called when an exception occurs and no exceptionhandler is set, and can be called by a custom exceptionhandler that wants to defer to the default behavior.
context parameter has the same meaning as in
call_exception_handler().
AbstractEventLoop.call_exception_handler(context)¶Call the current event loop exception handler.
context is a
dictobject containing the following keys(new keys may be introduced later):‘message’: Error message;
‘exception’ (optional): Exception object;
‘future’ (optional):
asyncio.Futureinstance;‘handle’ (optional):
asyncio.Handleinstance;‘protocol’ (optional):Protocol instance;
‘transport’ (optional):Transport instance;
‘socket’ (optional):
socket.socketinstance.
Note
Note: this method should not be overloaded in subclassedevent loops. For any custom exception handling, use
set_exception_handler()method.
18.5.1.15.Debug mode¶
AbstractEventLoop.get_debug()¶Get the debug mode (
bool) of the event loop.The default value is
Trueif the environment variablePYTHONASYNCIODEBUGis set to a non-empty string,Falseotherwise.New in version 3.4.2.
AbstractEventLoop.set_debug(enabled: bool)¶Set the debug mode of the event loop.
New in version 3.4.2.
See also
18.5.1.16.Server¶
- class
asyncio.Server¶ Server listening on sockets.
Object created by the
AbstractEventLoop.create_server()method and thestart_server()function. Don’t instantiate the class directly.close()¶Stop serving: close listening sockets and set the
socketsattribute toNone.The sockets that represent existing incoming client connections are leftopen.
The server is closed asynchronously, use the
wait_closed()coroutine to wait until the server is closed.
sockets¶List of
socket.socketobjects the server is listening to, orNoneif the server is closed.
18.5.1.17.Handle¶
- class
asyncio.Handle¶ A callback wrapper object returned by
AbstractEventLoop.call_soon(),AbstractEventLoop.call_soon_threadsafe(),AbstractEventLoop.call_later(),andAbstractEventLoop.call_at().cancel()¶Cancel the call. If the callback is already canceled or executed,this method has no effect.
18.5.1.18.Event loop examples¶
18.5.1.18.1.Hello World with call_soon()¶
Example using theAbstractEventLoop.call_soon() method to schedule acallback. The callback displays"HelloWorld" and then stops the eventloop:
importasynciodefhello_world(loop):print('Hello World')loop.stop()loop=asyncio.get_event_loop()# Schedule a call to hello_world()loop.call_soon(hello_world,loop)# Blocking call interrupted by loop.stop()loop.run_forever()loop.close()
See also
TheHello World coroutine exampleuses acoroutine.
18.5.1.18.2.Display the current date with call_later()¶
Example of callback displaying the current date every second. The callback usestheAbstractEventLoop.call_later() method to reschedule itself during 5seconds, and then stops the event loop:
importasyncioimportdatetimedefdisplay_date(end_time,loop):print(datetime.datetime.now())if(loop.time()+1.0)<end_time:loop.call_later(1,display_date,end_time,loop)else:loop.stop()loop=asyncio.get_event_loop()# Schedule the first call to display_date()end_time=loop.time()+5.0loop.call_soon(display_date,end_time,loop)# Blocking call interrupted by loop.stop()loop.run_forever()loop.close()
See also
Thecoroutine displaying the current date example uses acoroutine.
18.5.1.18.3.Watch a file descriptor for read events¶
Wait until a file descriptor received some data using theAbstractEventLoop.add_reader() method and then close the event loop:
importasynciotry:fromsocketimportsocketpairexceptImportError:fromasyncio.windows_utilsimportsocketpair# Create a pair of connected file descriptorsrsock,wsock=socketpair()loop=asyncio.get_event_loop()defreader():data=rsock.recv(100)print("Received:",data.decode())# We are done: unregister the file descriptorloop.remove_reader(rsock)# Stop the event looploop.stop()# Register the file descriptor for read eventloop.add_reader(rsock,reader)# Simulate the reception of data from the networkloop.call_soon(wsock.send,'abc'.encode())# Run the event looploop.run_forever()# We are done, close sockets and the event looprsock.close()wsock.close()loop.close()
See also
Theregister an open socket to wait for data using a protocol example uses a low-level protocol created by theAbstractEventLoop.create_connection() method.
Theregister an open socket to wait for data using streams example uses high-level streamscreated by theopen_connection() function in a coroutine.
18.5.1.18.4.Set signal handlers for SIGINT and SIGTERM¶
Register handlers for signalsSIGINT andSIGTERM usingtheAbstractEventLoop.add_signal_handler() method:
importasyncioimportfunctoolsimportosimportsignaldefask_exit(signame):print("got signal%s: exit"%signame)loop.stop()loop=asyncio.get_event_loop()forsignamein('SIGINT','SIGTERM'):loop.add_signal_handler(getattr(signal,signame),functools.partial(ask_exit,signame))print("Event loop running forever, press Ctrl+C to interrupt.")print("pid%s: send SIGINT or SIGTERM to exit."%os.getpid())try:loop.run_forever()finally:loop.close()
This example only works on UNIX.
