/README.rst

https://github.com/aio-libs/async-timeout · ReStructuredText · 99 lines · 64 code · 35 blank · 0 comment · 0 complexity · 5f66d98e9c652092a3098220ea26c17e MD5 · raw file

  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. async with timeout(1.5):
  22. await inner()
  23. 1. If ``inner()`` is executed faster than in ``1.5`` seconds nothing
  24. happens.
  25. 2. Otherwise ``inner()`` is cancelled internally by sending
  26. ``asyncio.CancelledError`` into but ``asyncio.TimeoutError`` is
  27. raised outside of context manager scope.
  28. *timeout* parameter could be ``None`` for skipping timeout functionality.
  29. Alternatively, ``timeout_at(when)`` can be used for scheduling
  30. at the absolute time::
  31. loop = asyncio.get_event_loop()
  32. now = loop.time()
  33. async with timeout_at(now + 1.5):
  34. await inner()
  35. Please note: it is not POSIX time but a time with
  36. undefined starting base, e.g. the time of the system power on.
  37. Context manager has ``.expired`` property for check if timeout happens
  38. exactly in context manager::
  39. async with timeout(1.5) as cm:
  40. await inner()
  41. print(cm.expired)
  42. The property is ``True`` if ``inner()`` execution is cancelled by
  43. timeout context manager.
  44. If ``inner()`` call explicitly raises ``TimeoutError`` ``cm.expired``
  45. is ``False``.
  46. The scheduled deadline time is available as ``.deadline`` property::
  47. async with timeout(1.5) as cm:
  48. cm.deadline
  49. Not finished yet timeout can be rescheduled by ``shift_by()``
  50. or ``shift_to()`` methods::
  51. async with timeout(1.5) as cm:
  52. cm.shift_by(1) # add another second on waiting
  53. cm.shift_to(loop.time() + 5) # reschedule to now+5 seconds
  54. Rescheduling is forbidden if the timeout is expired or after exit from ``async with``
  55. code block.
  56. Installation
  57. ------------
  58. ::
  59. $ pip install async-timeout
  60. The library is Python 3 only!
  61. Authors and License
  62. -------------------
  63. The module is written by Andrew Svetlov.
  64. It's *Apache 2* licensed and freely available.