File: //lib64/python3.6/site-packages/tornado/__pycache__/ioloop.cpython-36.pyc
3
��fe� � @ sH d Z ddlZddlZddlZddlZddlZddlZddlZddl Z ddl
Z
ddlZddlZddl
mZmZmZmZmZ ddlmZ ddlmZmZmZ ddlZddlmZmZmZmZmZmZm Z m!Z! ej"r�ddlm#Z#m$Z$ ddl%m&Z& ne'Z&G d d
� d
e&�Z(ed�Z)ede(d
�Z*G dd� de�Z+G dd� de'�Z,G dd� de'�Z-dS )a� An I/O event loop for non-blocking sockets.
In Tornado 6.0, `.IOLoop` is a wrapper around the `asyncio` event
loop, with a slightly different interface for historical reasons.
Applications can use either the `.IOLoop` interface or the underlying
`asyncio` event loop directly (unless compatibility with older
versions of Tornado is desired, in which case `.IOLoop` must be used).
Typical applications will use a single `IOLoop` object, accessed via
`IOLoop.current` class method. The `IOLoop.start` method (or
equivalently, `asyncio.AbstractEventLoop.run_forever`) should usually
be called at the end of the ``main()`` function. Atypical applications
may use more than one `IOLoop`, such as one `IOLoop` per thread, or
per `unittest` case.
� N)�Future� is_future�chain_future�future_set_exc_info�future_add_done_callback)�app_log)�Configurable�TimeoutError�
import_object)�Union�Any�Type�Optional�Callable�TypeVar�Tuple� Awaitable)�Dict�List)�Protocolc @ s( e Zd Zed�dd�Zdd�dd�ZdS )�_Selectable)�returnc C s d S )N� )�selfr r �/usr/lib64/python3.6/ioloop.py�filenoC s z_Selectable.filenoNc C s d S )Nr )r r r r �closeF s z_Selectable.close)�__name__�
__module__�__qualname__�intr r r r r r r B s r �_T�_S)Zboundc s� e Zd ZdZdZdZdZdZe� Z e
dedd�� fd d
��Ze
d d�dd
��Zdd�dd�Ze
dd�dd��Zeje
d d�dd���Zeje
dheed d�dd���Ze
dieed d�dd��Zdd�dd�Ze
dd�dd��Zdd�dd�Ze
ee d�dd��Ze
ee d�d d!��Zdjedd"�d#d$�Zdkedd&�d'd(�Zejee eegdf edd)�d*d+��Z!eje"e e"egdf edd)�d,d+��Z!e#ee$f e dl edd)�d.d+�Z!e#ee$f edd/�d0d1�Z%e#ee$f dd2�d3d4�Z&dd�d5d6�Z'dd�d7d8�Z(dd�d9d:�Z)dme e*ed;�d<d=�Z+e*d�d>d?�Z,e#e*e-j.f e dn eee/d@�dAdB�Z0e*e do eee/dC�dDdE�Z1e*e dp eee/dF�dGdH�Z2e/ddI�dJdK�Z3e eeddL�dMdN�Z4e eeddL�dOdP�Z5e eeddL�dQdR�Z6dSe dTgdf ddU�dVdW�Z7ee8j9j: e d-e;f ee<e; dX�dYdZ�Z=e8j9j:dd[�d\d]�Z>e g ef dd^�d_d`�Z?e@dda�dbdc�ZAe#ee$f eBee#ee$f f d2�ddde�ZCe#ee$f dd2�dfdg�ZD� ZES )q�IOLoopa� An I/O event loop.
As of Tornado 6.0, `IOLoop` is a wrapper around the `asyncio` event
loop.
Example usage for a simple TCP server:
.. testcode::
import errno
import functools
import socket
import tornado.ioloop
from tornado.iostream import IOStream
async def handle_connection(connection, address):
stream = IOStream(connection)
message = await stream.read_until_close()
print("message from client:", message.decode().strip())
def connection_ready(sock, fd, events):
while True:
try:
connection, address = sock.accept()
except socket.error as e:
if e.args[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
raise
return
connection.setblocking(0)
io_loop = tornado.ioloop.IOLoop.current()
io_loop.spawn_callback(handle_connection, connection, address)
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(0)
sock.bind(("", 8888))
sock.listen(128)
io_loop = tornado.ioloop.IOLoop.current()
callback = functools.partial(connection_ready, sock)
io_loop.add_handler(sock.fileno(), callback, io_loop.READ)
io_loop.start()
.. testoutput::
:hide:
By default, a newly-constructed `IOLoop` becomes the thread's current
`IOLoop`, unless there already is a current `IOLoop`. This behavior
can be controlled with the ``make_current`` argument to the `IOLoop`
constructor: if ``make_current=True``, the new `IOLoop` will always
try to become current and it raises an error if there is already a
current instance. If ``make_current=False``, the new `IOLoop` will
not try to become current.
In general, an `IOLoop` cannot survive a fork or be shared across
processes in any way. When multiple processes are being used, each
process should create its own `IOLoop`, which also implies that
any objects which depend on the `IOLoop` (such as
`.AsyncHTTPClient`) must also be created in the child processes.
As a guideline, anything that starts processes (including the
`tornado.process` and `multiprocessing` modules) should do so as
early as possible, ideally the first thing the application does
after loading its configuration in ``main()``.
.. versionchanged:: 4.2
Added the ``make_current`` keyword argument to the `IOLoop`
constructor.
.. versionchanged:: 5.0
Uses the `asyncio` event loop by default. The
``IOLoop.configure`` method cannot be used on Python 3 except
to redundantly specify the `asyncio` event loop.
r � � � z$Union[None, str, Type[Configurable]]N)�impl�kwargsr c s\ t d k rDddlm} t|t�r&t|�}t|t�rDt||� rDtd��t t
| �j|f|� d S )Nr )�BaseAsyncIOLoopz5only AsyncIOLoop is allowed when asyncio is available)�asyncio�tornado.platform.asyncior) �
isinstance�strr
�type�
issubclass�RuntimeError�superr# � configure)�clsr'