Low-level API Index

فهرست فصل‌ها دانلود آرشیوناشر

Python 3.14 Docs · نسخهٔ 3.14.10 · PSF License

===================

Low-level API Index

This page lists all low-level asyncio APIs.

Obtaining the Event Loop

:widths: 50 50 :class: full-width-table

  • - asyncio.get_running_loop
  • The preferred function to get the running event loop.
  • - asyncio.get_event_loop
  • Get an event loop instance (running or current via the current policy).
  • - asyncio.set_event_loop
  • Set the event loop as current via the current policy.
  • - asyncio.new_event_loop
  • Create a new event loop.
  • Using asyncio.get_running_loop() <asyncio_example_future>.

Event Loop Methods

See also the main documentation section about the asyncio-event-loop-methods.

:widths: 50 50 :class: full-width-table

  • - loop.run_until_complete
  • Run a Future/Task/awaitable until complete.
  • - loop.run_forever
  • Run the event loop forever.
  • - loop.stop
  • Stop the event loop.
  • - loop.close
  • Close the event loop.
  • - loop.is_running
  • Return `True` if the event loop is running.
  • - loop.is_closed
  • Return `True` if the event loop is closed.
  • - `await loop.shutdown_asyncgens`
  • Close asynchronous generators.

:widths: 50 50 :class: full-width-table

  • - loop.set_debug
  • Enable or disable the debug mode.
  • - loop.get_debug
  • Get the current debug mode.

:widths: 50 50 :class: full-width-table

  • - loop.call_soon
  • Invoke a callback soon.
  • - loop.call_soon_threadsafe
  • A thread-safe variant of loop.call_soon.
  • - loop.call_later
  • Invoke a callback *after* the given time.
  • - loop.call_at
  • Invoke a callback *at* the given time.

:widths: 50 50 :class: full-width-table

  • - `await loop.run_in_executor`
  • Run a CPU-bound or other blocking function in

a concurrent.futures executor.

  • - loop.set_default_executor
  • Set the default executor for loop.run_in_executor.

:widths: 50 50 :class: full-width-table

  • - loop.create_future
  • Create a Future object.
  • - loop.create_task
  • Schedule coroutine as a Task.
  • - loop.set_task_factory
  • Set a factory used by loop.create_task to

create Tasks <Task>.

  • - loop.get_task_factory
  • Get the factory loop.create_task uses

to create Tasks <Task>.

:widths: 50 50 :class: full-width-table

  • - `await loop.getaddrinfo`
  • Asynchronous version of socket.getaddrinfo.
  • - `await loop.getnameinfo`
  • Asynchronous version of socket.getnameinfo.

:widths: 50 50 :class: full-width-table

  • - `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.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.

:widths: 50 50 :class: full-width-table

  • - `await loop.sock_recv`
  • Receive data from the ~socket.socket.
  • - `await loop.sock_recv_into`
  • Receive data from the ~socket.socket into a buffer.
  • - `await loop.sock_recvfrom`
  • Receive a datagram from the ~socket.socket.
  • - `await loop.sock_recvfrom_into`
  • Receive a datagram from the ~socket.socket into a buffer.
  • - `await loop.sock_sendall`
  • Send data to the ~socket.socket.
  • - `await loop.sock_sendto`
  • Send a datagram via the ~socket.socket to the given address.
  • - `await loop.sock_connect`
  • Connect the ~socket.socket.
  • - `await loop.sock_accept`
  • Accept a ~socket.socket connection.
  • - `await loop.sock_sendfile`
  • Send a file over the ~socket.socket.
  • - loop.add_reader
  • Start watching a file descriptor for read availability.
  • - loop.remove_reader
  • Stop watching a file descriptor for read availability.
  • - loop.add_writer
  • Start watching a file descriptor for write availability.
  • - loop.remove_writer
  • Stop watching a file descriptor for write availability.

:widths: 50 50 :class: full-width-table

  • - loop.add_signal_handler
  • Add a handler for a signal.
  • - loop.remove_signal_handler
  • Remove a handler for a signal.

:widths: 50 50 :class: full-width-table

  • - loop.subprocess_exec
  • Spawn a subprocess.
  • - loop.subprocess_shell
  • Spawn a subprocess from a shell command.

:widths: 50 50 :class: full-width-table

  • - loop.call_exception_handler
  • Call the exception handler.
  • - loop.set_exception_handler
  • Set a new exception handler.
  • - loop.get_exception_handler
  • Get the current exception handler.
  • - loop.default_exception_handler
  • The default exception handler implementation.
  • :ref:`Using asyncio.new_event_loop() and loop.run_forever()

<asyncio_example_lowlevel_helloworld>`.

  • Using loop.call_later() <asyncio_example_call_later>.
  • Using `loop.create_connection()` to implement

an echo-client <asyncio_example_tcp_echo_client_protocol>.

  • Using `loop.create_connection()` to

connect a socket <asyncio_example_create_connection>.

  • :ref:`Using add_reader() to watch an FD for read events

<asyncio_example_watch_fd>`.

  • Using loop.add_signal_handler() <asyncio_example_unix_signals>.
  • Using loop.subprocess_exec() <asyncio_example_subprocess_proto>.

Transports

All transports implement the following methods:

:widths: 50 50 :class: full-width-table

  • - transport.close() <BaseTransport.close>
  • Close the transport.
  • - transport.is_closing() <BaseTransport.is_closing>
  • Return `True` if the transport is closing or is closed.
  • - transport.get_extra_info() <BaseTransport.get_extra_info>
  • Request for information about the transport.
  • - transport.set_protocol() <BaseTransport.set_protocol>
  • Set a new protocol.
  • - transport.get_protocol() <BaseTransport.get_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:

:widths: 50 50 :class: full-width-table

  • - transport.is_reading() <ReadTransport.is_reading>
  • Return `True` if the transport is receiving.
  • - transport.pause_reading() <ReadTransport.pause_reading>
  • Pause receiving.
  • - transport.resume_reading() <ReadTransport.resume_reading>
  • 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:

:widths: 50 50 :class: full-width-table

  • - transport.write() <WriteTransport.write>
  • Write data to the transport.
  • - transport.writelines() <WriteTransport.writelines>
  • Write buffers to the transport.
  • - transport.can_write_eof() <WriteTransport.can_write_eof>
  • Return True if the transport supports sending EOF.
  • - transport.write_eof() <WriteTransport.write_eof>
  • Close and send EOF after flushing buffered data.
  • - transport.abort() <WriteTransport.abort>
  • Close the transport immediately.
  • - :meth:`transport.get_write_buffer_size()

<WriteTransport.get_write_buffer_size>`

  • Return the current size of the output buffer.
  • - :meth:`transport.get_write_buffer_limits()

<WriteTransport.get_write_buffer_limits>`

  • Return high and low water marks for write flow control.
  • - :meth:`transport.set_write_buffer_limits()

<WriteTransport.set_write_buffer_limits>`

  • Set new high and low water marks for write flow control.

Transports returned by loop.create_datagram_endpoint:

:widths: 50 50 :class: full-width-table

  • - transport.sendto() <DatagramTransport.sendto>
  • Send data to the remote peer.
  • - transport.abort() <DatagramTransport.abort>
  • Close the transport immediately.

Low-level transport abstraction over subprocesses. Returned by loop.subprocess_exec and loop.subprocess_shell:

:widths: 50 50 :class: full-width-table

  • - transport.get_pid() <SubprocessTransport.get_pid>
  • Return the subprocess process id.
  • - :meth:`transport.get_pipe_transport()

<SubprocessTransport.get_pipe_transport>`

  • Return the transport for the requested communication pipe

(*stdin*, *stdout*, or *stderr*).

  • - transport.get_returncode() <SubprocessTransport.get_returncode>
  • Return the subprocess return code.
  • - transport.kill() <SubprocessTransport.kill>
  • Kill the subprocess.
  • - transport.send_signal() <SubprocessTransport.send_signal>
  • Send a signal to the subprocess.
  • - transport.terminate() <SubprocessTransport.terminate>
  • Stop the subprocess.
  • - transport.close() <SubprocessTransport.close>
  • Kill the subprocess and close all pipes.

Protocols

Protocol classes can implement the following callback methods:

:widths: 50 50 :class: full-width-table

  • - `callback connection_made() <BaseProtocol.connection_made>`
  • Called when a connection is made.
  • - `callback connection_lost() <BaseProtocol.connection_lost>`
  • Called when the connection is lost or closed.
  • - `callback pause_writing() <BaseProtocol.pause_writing>`
  • Called when the transport's buffer goes over the high water mark.
  • - `callback resume_writing() <BaseProtocol.resume_writing>`
  • Called when the transport's buffer drains below the low water mark.

:widths: 50 50 :class: full-width-table

  • - `callback data_received() <Protocol.data_received>`
  • Called when some data is received.
  • - `callback eof_received() <Protocol.eof_received>`
  • Called when an EOF is received.

:widths: 50 50 :class: full-width-table

  • - `callback get_buffer() <BufferedProtocol.get_buffer>`
  • Called to allocate a new receive buffer.
  • - `callback buffer_updated() <BufferedProtocol.buffer_updated>`
  • Called when the buffer was updated with the received data.
  • - `callback eof_received() <BufferedProtocol.eof_received>`
  • Called when an EOF is received.

:widths: 50 50 :class: full-width-table

  • - `callback :meth:datagram_received()

<DatagramProtocol.datagram_received>`

  • Called when a datagram is received.
  • - `callback error_received() <DatagramProtocol.error_received>`
  • Called when a previous send or receive operation raises an

OSError.

:widths: 50 50 :class: full-width-table

  • - `callback ~SubprocessProtocol.pipe_data_received`
  • Called when the child process writes data into its

*stdout* or *stderr* pipe.

  • - `callback ~SubprocessProtocol.pipe_connection_lost`
  • Called when one of the pipes communicating with

the child process is closed.

  • - `callback :meth:process_exited()

<SubprocessProtocol.process_exited>`

  • Called when the child process has exited. It can be called before

~SubprocessProtocol.pipe_data_received and ~SubprocessProtocol.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 <asyncio-policies> for more details.

:widths: 50 50 :class: full-width-table

  • - asyncio.get_event_loop_policy
  • Return the current process-wide policy.
  • - asyncio.set_event_loop_policy
  • Set a new process-wide policy.
  • - AbstractEventLoopPolicy
  • Base class for policy objects.