| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: async-timeout |
| 3 |
Version: 5.0.1 |
| 4 |
Summary: Timeout context manager for asyncio programs |
| 5 |
Home-page: https://github.com/aio-libs/async-timeout |
| 6 |
Author: Andrew Svetlov <[email protected]> |
| 7 |
Author-email: [email protected] |
| 8 |
License: Apache 2 |
| 9 |
Project-URL: Chat: Gitter, https://gitter.im/aio-libs/Lobby |
| 10 |
Project-URL: CI: GitHub Actions, https://github.com/aio-libs/async-timeout/actions |
| 11 |
Project-URL: Coverage: codecov, https://codecov.io/github/aio-libs/async-timeout |
| 12 |
Project-URL: GitHub: issues, https://github.com/aio-libs/async-timeout/issues |
| 13 |
Project-URL: GitHub: repo, https://github.com/aio-libs/async-timeout |
| 14 |
Classifier: Development Status :: 5 - Production/Stable |
| 15 |
Classifier: Topic :: Software Development :: Libraries |
| 16 |
Classifier: Framework :: AsyncIO |
| 17 |
Classifier: Intended Audience :: Developers |
| 18 |
Classifier: License :: OSI Approved :: Apache Software License |
| 19 |
Classifier: Programming Language :: Python |
| 20 |
Classifier: Programming Language :: Python :: 3 |
| 21 |
Classifier: Programming Language :: Python :: 3 :: Only |
| 22 |
Requires-Python: >=3.8 |
| 23 |
Description-Content-Type: text/x-rst |
| 24 |
License-File: LICENSE |
| 25 |
|
| 26 |
async-timeout |
| 27 |
============= |
| 28 |
.. image:: https://travis-ci.com/aio-libs/async-timeout.svg?branch=master |
| 29 |
:target: https://travis-ci.com/aio-libs/async-timeout |
| 30 |
.. image:: https://codecov.io/gh/aio-libs/async-timeout/branch/master/graph/badge.svg |
| 31 |
:target: https://codecov.io/gh/aio-libs/async-timeout |
| 32 |
.. image:: https://img.shields.io/pypi/v/async-timeout.svg |
| 33 |
:target: https://pypi.python.org/pypi/async-timeout |
| 34 |
.. image:: https://badges.gitter.im/Join%20Chat.svg |
| 35 |
:target: https://gitter.im/aio-libs/Lobby |
| 36 |
:alt: Chat on Gitter |
| 37 |
|
| 38 |
asyncio-compatible timeout context manager. |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
DEPRECATED |
| 43 |
---------- |
| 44 |
|
| 45 |
This library has effectively been upstreamed into Python 3.11+. |
| 46 |
|
| 47 |
Therefore this library is considered deprecated and no longer actively supported. |
| 48 |
|
| 49 |
Version 5.0+ provides dual-mode when executed on Python 3.11+: |
| 50 |
``asyncio_timeout.Timeout`` is fully compatible with ``asyncio.Timeout`` *and* old |
| 51 |
versions of the library. |
| 52 |
|
| 53 |
Anyway, using upstream is highly recommended. ``asyncio_timeout`` exists only for the |
| 54 |
sake of backward compatibility, easy supporting both old and new Python by the same |
| 55 |
code, and easy misgration. |
| 56 |
|
| 57 |
If rescheduling API is not important and only ``async with timeout(...): ...`` functionality is required, |
| 58 |
a user could apply conditional import:: |
| 59 |
|
| 60 |
if sys.version_info >= (3, 11): |
| 61 |
from asyncio import timeout, timeout_at |
| 62 |
else: |
| 63 |
from async_timeout import timeout, timeout_at |
| 64 |
|
| 65 |
|
| 66 |
Usage example |
| 67 |
------------- |
| 68 |
|
| 69 |
|
| 70 |
The context manager is useful in cases when you want to apply timeout |
| 71 |
logic around block of code or in cases when ``asyncio.wait_for()`` is |
| 72 |
not suitable. Also it's much faster than ``asyncio.wait_for()`` |
| 73 |
because ``timeout`` doesn't create a new task. |
| 74 |
|
| 75 |
The ``timeout(delay, *, loop=None)`` call returns a context manager |
| 76 |
that cancels a block on *timeout* expiring:: |
| 77 |
|
| 78 |
from async_timeout import timeout |
| 79 |
async with timeout(1.5): |
| 80 |
await inner() |
| 81 |
|
| 82 |
1. If ``inner()`` is executed faster than in ``1.5`` seconds nothing |
| 83 |
happens. |
| 84 |
2. Otherwise ``inner()`` is cancelled internally by sending |
| 85 |
``asyncio.CancelledError`` into but ``asyncio.TimeoutError`` is |
| 86 |
raised outside of context manager scope. |
| 87 |
|
| 88 |
*timeout* parameter could be ``None`` for skipping timeout functionality. |
| 89 |
|
| 90 |
|
| 91 |
Alternatively, ``timeout_at(when)`` can be used for scheduling |
| 92 |
at the absolute time:: |
| 93 |
|
| 94 |
loop = asyncio.get_event_loop() |
| 95 |
now = loop.time() |
| 96 |
|
| 97 |
async with timeout_at(now + 1.5): |
| 98 |
await inner() |
| 99 |
|
| 100 |
|
| 101 |
Please note: it is not POSIX time but a time with |
| 102 |
undefined starting base, e.g. the time of the system power on. |
| 103 |
|
| 104 |
|
| 105 |
Context manager has ``.expired()`` / ``.expired`` for check if timeout happens |
| 106 |
exactly in context manager:: |
| 107 |
|
| 108 |
async with timeout(1.5) as cm: |
| 109 |
await inner() |
| 110 |
print(cm.expired()) # recommended api |
| 111 |
print(cm.expired) # compatible api |
| 112 |
|
| 113 |
The property is ``True`` if ``inner()`` execution is cancelled by |
| 114 |
timeout context manager. |
| 115 |
|
| 116 |
If ``inner()`` call explicitly raises ``TimeoutError`` ``cm.expired`` |
| 117 |
is ``False``. |
| 118 |
|
| 119 |
The scheduled deadline time is available as ``.when()`` / ``.deadline``:: |
| 120 |
|
| 121 |
async with timeout(1.5) as cm: |
| 122 |
cm.when() # recommended api |
| 123 |
cm.deadline # compatible api |
| 124 |
|
| 125 |
Not finished yet timeout can be rescheduled by ``shift()`` |
| 126 |
or ``update()`` methods:: |
| 127 |
|
| 128 |
async with timeout(1.5) as cm: |
| 129 |
# recommended api |
| 130 |
cm.reschedule(cm.when() + 1) # add another second on waiting |
| 131 |
# compatible api |
| 132 |
cm.shift(1) # add another second on waiting |
| 133 |
cm.update(loop.time() + 5) # reschedule to now+5 seconds |
| 134 |
|
| 135 |
Rescheduling is forbidden if the timeout is expired or after exit from ``async with`` |
| 136 |
code block. |
| 137 |
|
| 138 |
|
| 139 |
Disable scheduled timeout:: |
| 140 |
|
| 141 |
async with timeout(1.5) as cm: |
| 142 |
cm.reschedule(None) # recommended api |
| 143 |
cm.reject() # compatible api |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
Installation |
| 148 |
------------ |
| 149 |
|
| 150 |
:: |
| 151 |
|
| 152 |
$ pip install async-timeout |
| 153 |
|
| 154 |
The library is Python 3 only! |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
Authors and License |
| 159 |
------------------- |
| 160 |
|
| 161 |
The module is written by Andrew Svetlov. |
| 162 |
|
| 163 |
It's *Apache 2* licensed and freely available. |