asyncio
本页列出了所有低级 asyncio API。
asyncio.get_running_loop()
The preferred function to get the running event loop.
asyncio.get_event_loop()
asyncio.set_event_loop()
asyncio.new_event_loop()
范例
使用 asyncio.get_running_loop() .
See also the main documentation section about the 事件循环方法 .
Lifecycle
loop.run_until_complete()
loop.run_forever()
loop.stop()
loop.close()
loop.is_running()
返回 True if the event loop is running.
True
loop.is_closed()
返回 True if the event loop is closed.
await loop.shutdown_asyncgens()
await
loop.shutdown_asyncgens()
调试
loop.set_debug()
loop.get_debug()
Scheduling Callbacks
loop.call_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.
线程/进程池
await loop.run_in_executor()
loop.run_in_executor()
Run a CPU-bound or other blocking function in a concurrent.futures executor.
concurrent.futures
loop.set_default_executor()
Set the default executor for loop.run_in_executor() .
任务和未来
loop.create_future()
创建 Future 对象。
Future
loop.create_task()
Schedule coroutine as a Task .
Task
loop.set_task_factory()
Set a factory used by loop.create_task() to create Tasks .
Tasks
loop.get_task_factory()
Get the factory loop.create_task() uses to create Tasks .
DNS
await loop.getaddrinfo()
loop.getaddrinfo()
Asynchronous version of socket.getaddrinfo() .
socket.getaddrinfo()
await loop.getnameinfo()
loop.getnameinfo()
Asynchronous version of socket.getnameinfo() .
socket.getnameinfo()
网络和 IPC (进程间通信)
await loop.create_connection()
loop.create_connection()
await loop.create_server()
loop.create_server()
await loop.create_unix_connection()
loop.create_unix_connection()
await loop.create_unix_server()
loop.create_unix_server()
await loop.connect_accepted_socket()
loop.connect_accepted_socket()
包裹 socket 成 (transport, protocol) 对。
socket
(transport, protocol)
await loop.create_datagram_endpoint()
loop.create_datagram_endpoint()
await loop.sendfile()
loop.sendfile()
await loop.start_tls()
loop.start_tls()
await loop.connect_read_pipe()
loop.connect_read_pipe()
Wrap a read end of a pipe into a (transport, protocol) 对。
await loop.connect_write_pipe()
loop.connect_write_pipe()
Wrap a write end of a pipe into a (transport, protocol) 对。
套接字
await loop.sock_recv()
loop.sock_recv()
Receive data from the socket .
await loop.sock_recv_into()
loop.sock_recv_into()
Receive data from the socket into a buffer.
await loop.sock_recvfrom()
loop.sock_recvfrom()
Receive a datagram from the socket .
await loop.sock_recvfrom_into()
loop.sock_recvfrom_into()
Receive a datagram from the socket into a buffer.
await loop.sock_sendall()
loop.sock_sendall()
Send data to the socket .
await loop.sock_sendto()
loop.sock_sendto()
Send a datagram via the socket to the given address.
await loop.sock_connect()
loop.sock_connect()
连接 socket .
await loop.sock_accept()
loop.sock_accept()
Accept a socket 连接。
await loop.sock_sendfile()
loop.sock_sendfile()
Send a file over the socket .
loop.add_reader()
loop.remove_reader()
loop.add_writer()
loop.remove_writer()
Unix 信号
loop.add_signal_handler()
Add a handler for a signal .
signal
loop.remove_signal_handler()
Remove a handler for a signal .
子进程
loop.subprocess_exec()
loop.subprocess_shell()
错误处理
loop.call_exception_handler()
loop.set_exception_handler()
loop.get_exception_handler()
loop.default_exception_handler()
Using asyncio.new_event_loop() and loop.run_forever() .
使用 loop.call_later() .
使用 loop.create_connection() 以实现 an echo-client .
使用 loop.create_connection() to connect a socket .
Using add_reader() to watch an FD for read events .
Using loop.add_signal_handler() .
Using loop.subprocess_exec() .
All transports implement the following methods:
transport.close()
transport.is_closing()
返回 True if the transport is closing or is closed.
transport.get_extra_info()
transport.set_protocol()
transport.get_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
transport.is_reading()
返回 True if the transport is receiving.
transport.pause_reading()
transport.resume_reading()
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
transport.write()
transport.writelines()
transport.can_write_eof()
返回 True if the transport supports sending EOF.
transport.write_eof()
transport.abort()
transport.get_write_buffer_size()
transport.get_write_buffer_limits()
transport.set_write_buffer_limits()
Transports returned by loop.create_datagram_endpoint() :
数据报传输
transport.sendto()
Low-level transport abstraction over subprocesses. Returned by loop.subprocess_exec() and loop.subprocess_shell() :
子进程传输
transport.get_pid()
transport.get_pipe_transport()
Return the transport for the requested communication pipe ( stdin , stdout ,或 stderr ).
transport.get_returncode()
transport.kill()
transport.send_signal()
transport.terminate()
Protocol classes can implement the following callback methods :
callback connection_made()
callback
connection_made()
callback connection_lost()
connection_lost()
callback pause_writing()
pause_writing()
callback resume_writing()
resume_writing()
Streaming Protocols (TCP, Unix Sockets, Pipes)
callback data_received()
data_received()
callback eof_received()
eof_received()
缓冲流协议
callback get_buffer()
get_buffer()
callback buffer_updated()
buffer_updated()
数据报协议
callback datagram_received()
datagram_received()
callback error_received()
error_received()
Called when a previous send or receive operation raises an OSError .
OSError
子进程协议
callback pipe_data_received()
pipe_data_received()
Called when the child process writes data into its stdout or stderr pipe.
callback pipe_connection_lost()
pipe_connection_lost()
Called when one of the pipes communicating with the child process is closed.
callback process_exited()
process_exited()
Called when the child process has exited. It can be called before pipe_data_received() and pipe_connection_lost() 方法。
Policies is a low-level mechanism to alter the behavior of functions like asyncio.get_event_loop() . See also the main policies section 了解更多细节。
Accessing Policies
asyncio.get_event_loop_policy()
asyncio.set_event_loop_policy()
AbstractEventLoopPolicy
高级 API 索引
采用 asyncio 开发
键入搜索术语或模块、类、函数名称。