Low-level API Index¶
This page lists all low-level asyncio APIs.
Obtaining the Event Loop¶
The preferred function to get the running event loop.
Get an event loop instance (running or current via the current policy).
Set the event loop as current via the current policy.
Create a new event loop.
Examples
Event Loop Methods¶
See also the main documentation section about the Event Loop Methods.
Lifecycle
Run a Future/Task/awaitable until complete.
Run the event loop forever.
Stop the event loop.
Close the event loop.
Return True if the event loop is running.
Return True if the event loop is closed.
await loop.shutdown_asyncgens()
Close asynchronous generators.
Debugging
Enable or disable the debug mode.
Get the current debug mode.
Scheduling Callbacks
Invoke a callback soon.
A thread-safe variant of loop.call_soon().
Invoke a callback after the given time.
Invoke a callback at the given time.
Thread/Interpreter/Process Pool
await loop.run_in_executor()
Run a CPU-bound or other blocking function in
a concurrent.futures executor.
Set the default executor for loop.run_in_executor().
Tasks and Futures
Create a Future object.
Schedule coroutine as a Task.
Set a factory used by loop.create_task() to
create Tasks.
Get the factory loop.create_task() uses
to create Tasks.
DNS
await loop.getaddrinfo()
Asynchronous version of socket.getaddrinfo().
await loop.getnameinfo()
Asynchronous version of socket.getnameinfo().
Networking and IPC
await loop.create_connection()
Open a TCP connection.
await loop.create_server()
Create a TCP server.
await loop.create_unix_connection()
Open a Unix socket connection.
await loop.create_unix_server()
Create a Unix socket server.
await loop.connect_accepted_socket()
Wrap a socket into a (transport, protocol)
pair.
await loop.create_datagram_endpoint()
Open a datagram (UDP) connection.
await loop.sendfile()
Send a file over a transport.
await loop.start_tls()
Upgrade an existing connection to TLS.
await loop.connect_read_pipe()
Wrap a read end of a pipe into a (transport, protocol) pair.
await loop.connect_write_pipe()
Wrap a write end of a pipe into a (transport, protocol) pair.
Sockets
await loop.sock_recv()
Receive data from the socket.
await loop.sock_recv_into()
Receive data from the socket into a buffer.
await loop.sock_recvfrom()
Receive a datagram from the socket.
await loop.sock_recvfrom_into()
Receive a datagram from the socket into a buffer.
await loop.sock_sendall()
Send data to the socket.
await loop.sock_sendto()
Send a datagram via the socket to the given address.
await loop.sock_connect()
Connect the socket.
await loop.sock_accept()
Accept a socket connection.
await loop.sock_sendfile()
Send a file over the socket.
Start watching a file descriptor for read availability.
Stop watching a file descriptor for read availability.
Start watching a file descriptor for write availability.
Stop watching a file descriptor for write availability.
Unix Signals
Add a handler for a signal.
Remove a handler for a signal.
Subprocesses
Spawn a subprocess.
Spawn a subprocess from a shell command.
Error Handling
Call the exception handler.
Set a new exception handler.
Get the current exception handler.
loop.default_exception_handler()
The default exception handler implementation.
Examples
Using
loop.create_connection()to implement an echo-client.Using
loop.create_connection()to connect a socket.
Transports¶
All transports implement the following methods:
Close the transport.
Return True if the transport is closing or is closed.
Request for information about the transport.
Set a new protocol.
Return the current protocol.
Transports that can receive data (TCP and Unix connections,
pipes, etc). Returned from methods like
loop.create_connection(), loop.create_unix_connection(),
loop.connect_read_pipe(), etc:
Read Transports
Return True if the transport is receiving.
Pause receiving.
Resume receiving.
Transports that can Send data (TCP and Unix connections,
pipes, etc). Returned from methods like
loop.create_connection(), loop.create_unix_connection(),
loop.connect_write_pipe(), etc:
Write Transports
Write data to the transport.
Write buffers to the transport.
Return True if the transport supports sending EOF.
Close and send EOF after flushing buffered data.
Close the transport immediately.
transport.get_write_buffer_size()
Return the current size of the output buffer.
transport.get_write_buffer_limits()
Return high and low water marks for write flow control.
transport.set_write_buffer_limits()
Set new high and low water marks for write flow control.
Transports returned by loop.create_datagram_endpoint():
Datagram Transports
Send data to the remote peer.
Close the transport immediately.
Low-level transport abstraction over subprocesses.
Returned by loop.subprocess_exec() and
loop.subprocess_shell():
Subprocess Transports
Return the subprocess process id.
transport.get_pipe_transport()
Return the transport for the requested communication pipe (stdin, stdout, or stderr).
Return the subprocess return code.
Kill the subprocess.
Send a signal to the subprocess.
Stop the subprocess.
Kill the subprocess and close all pipes.
Protocols¶
Protocol classes can implement the following callback methods:
callback connection_made()
Called when a connection is made.
callback connection_lost()
Called when the connection is lost or closed.
callback pause_writing()
Called when the transport’s buffer goes over the high water mark.
callback resume_writing()
Called when the transport’s buffer drains below the low water mark.
Streaming Protocols (TCP, Unix Sockets, Pipes)
callback data_received()
Called when some data is received.
callback eof_received()
Called when an EOF is received.
Buffered Streaming Protocols
callback get_buffer()
Called to allocate a new receive buffer.
callback buffer_updated()
Called when the buffer was updated with the received data.
callback eof_received()
Called when an EOF is received.
Datagram Protocols
callback datagram_received()
Called when a datagram is received.
callback error_received()
Called when a previous send or receive operation raises an
OSError.
Subprocess Protocols
callback pipe_data_received()
Called when the child process writes data into its stdout or stderr pipe.
callback pipe_connection_lost()
Called when one of the pipes communicating with the child process is closed.
callback process_exited()
Called when the child process has exited. It can be called before
pipe_data_received() and
pipe_connection_lost() methods.
Event Loop Policies¶
Policies is a low-level mechanism to alter the behavior of
functions like asyncio.get_event_loop(). See also
the main policies section for more
details.
Accessing Policies
asyncio.get_event_loop_policy()
Return the current process-wide policy.
asyncio.set_event_loop_policy()
Set a new process-wide policy.
Base class for policy objects.