README.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. async-timeout
  2. =============
  3. .. image:: https://travis-ci.com/aio-libs/async-timeout.svg?branch=master
  4. :target: https://travis-ci.com/aio-libs/async-timeout
  5. .. image:: https://codecov.io/gh/aio-libs/async-timeout/branch/master/graph/badge.svg
  6. :target: https://codecov.io/gh/aio-libs/async-timeout
  7. .. image:: https://img.shields.io/pypi/v/async-timeout.svg
  8. :target: https://pypi.python.org/pypi/async-timeout
  9. .. image:: https://badges.gitter.im/Join%20Chat.svg
  10. :target: https://gitter.im/aio-libs/Lobby
  11. :alt: Chat on Gitter
  12. asyncio-compatible timeout context manager.
  13. Usage example
  14. -------------
  15. The context manager is useful in cases when you want to apply timeout
  16. logic around block of code or in cases when ``asyncio.wait_for()`` is
  17. not suitable. Also it's much faster than ``asyncio.wait_for()``
  18. because ``timeout`` doesn't create a new task.
  19. The ``timeout(delay, *, loop=None)`` call returns a context manager
  20. that cancels a block on *timeout* expiring::
  21. from async_timeout import timeout
  22. async with timeout(1.5):
  23. await inner()
  24. 1. If ``inner()`` is executed faster than in ``1.5`` seconds nothing
  25. happens.
  26. 2. Otherwise ``inner()`` is cancelled internally by sending
  27. ``asyncio.CancelledError`` into but ``asyncio.TimeoutError`` is
  28. raised outside of context manager scope.
  29. *timeout* parameter could be ``None`` for skipping timeout functionality.
  30. Alternatively, ``timeout_at(when)`` can be used for scheduling
  31. at the absolute time::
  32. loop = asyncio.get_event_loop()
  33. now = loop.time()
  34. async with timeout_at(now + 1.5):
  35. await inner()
  36. Please note: it is not POSIX time but a time with
  37. undefined starting base, e.g. the time of the system power on.
  38. Context manager has ``.expired`` property for check if timeout happens
  39. exactly in context manager::
  40. async with timeout(1.5) as cm:
  41. await inner()
  42. print(cm.expired)
  43. The property is ``True`` if ``inner()`` execution is cancelled by
  44. timeout context manager.
  45. If ``inner()`` call explicitly raises ``TimeoutError`` ``cm.expired``
  46. is ``False``.
  47. The scheduled deadline time is available as ``.deadline`` property::
  48. async with timeout(1.5) as cm:
  49. cm.deadline
  50. Not finished yet timeout can be rescheduled by ``shift_by()``
  51. or ``shift_to()`` methods::
  52. async with timeout(1.5) as cm:
  53. cm.shift(1) # add another second on waiting
  54. cm.update(loop.time() + 5) # reschedule to now+5 seconds
  55. Rescheduling is forbidden if the timeout is expired or after exit from ``async with``
  56. code block.
  57. Installation
  58. ------------
  59. ::
  60. $ pip install async-timeout
  61. The library is Python 3 only!
  62. Authors and License
  63. -------------------
  64. The module is written by Andrew Svetlov.
  65. It's *Apache 2* licensed and freely available.