METADATA 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. Metadata-Version: 2.4
  2. Name: backports.asyncio.runner
  3. Version: 1.2.0
  4. Summary: Backport of asyncio.Runner, a context manager that controls event loop life cycle.
  5. Project-URL: Homepage, https://github.com/samypr100/backports.asyncio.runner
  6. Project-URL: Changelog, https://github.com/samypr100/backports.asyncio.runner/blob/main/CHANGELOG.md
  7. Author-email: samypr100 <3933065+samypr100@users.noreply.github.com>
  8. License: PSF-2.0
  9. License-File: LICENSE.md
  10. Keywords: asyncio,asyncio.Runner,backports,backports.asyncio,backports.asyncio.Runner
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Intended Audience :: Developers
  13. Classifier: License :: OSI Approved :: Python Software Foundation License
  14. Classifier: Operating System :: OS Independent
  15. Classifier: Programming Language :: Python
  16. Classifier: Programming Language :: Python :: 3 :: Only
  17. Classifier: Programming Language :: Python :: 3.8
  18. Classifier: Programming Language :: Python :: 3.9
  19. Classifier: Programming Language :: Python :: 3.10
  20. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  21. Classifier: Typing :: Typed
  22. Requires-Python: <3.11,>=3.8
  23. Description-Content-Type: text/markdown
  24. # backports.asyncio.runner
  25. [![PyPI version][project-badge]](https://pypi.org/project/backports.asyncio.runner)
  26. [![GitHub Actions][github-actions-badge]](https://github.com/samypr100/backports.asyncio.runner/actions/workflows/main.yml)
  27. [![Hatch][hatch-badge]](https://github.com/pypa/hatch)
  28. [![Ruff][ruff-badge]](https://github.com/astral-sh/ruff)
  29. [![Uv][uv-badge]](https://github.com/astral-sh/uv)
  30. [![Type checked with mypy][mypy-badge]](https://mypy-lang.org)
  31. [![Coverage][coverage-badge]](https://coverage.readthedocs.io)
  32. [project-badge]: https://badge.fury.io/py/backports.asyncio.runner.svg
  33. [github-actions-badge]: https://github.com/samypr100/backports.asyncio.runner/actions/workflows/main.yml/badge.svg
  34. [hatch-badge]: https://img.shields.io/badge/%F0%9F%A5%9A-Hatch-4051b5.svg
  35. [ruff-badge]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
  36. [uv-badge]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json
  37. [mypy-badge]: https://www.mypy-lang.org/static/mypy_badge.svg
  38. [coverage-badge]: https://gist.githubusercontent.com/samypr100/8682bd2df950670a45095c7c109a176e/raw/coverage.svg
  39. This is a backport of Python 3.11 [asyncio.Runner](https://docs.python.org/3/library/asyncio-runner.html#asyncio.Runner), a context manager that simplifies multiple async
  40. function calls in the same context.
  41. This backports provides full compatibility with Python 3.11 `asyncio.Runner` features, including `contextvars.Context` support.
  42. As such, the Python 3.11 test suite for `asyncio.Runner` is used as-is to test its functionality.
  43. This backport is meant to work with Python 3.8, 3.9, and 3.10 only. Users of Python 3.11 or above should
  44. not use this package directly.
  45. To install, you can use `python -m pip install backports.asyncio.runner` or your favorite python package manager.
  46. You might need to include a marker (e.g. `python_version < '3.11'`) to prevent installation on Python 3.11 or above.
  47. An example of the recommended way to use this context manager is in the below code snippet:
  48. ```python
  49. import sys
  50. if sys.version_info < (3, 11):
  51. from backports.asyncio.runner import Runner
  52. else:
  53. from asyncio import Runner
  54. async def echo(msg: str) -> None:
  55. print(f"Hello {msg}")
  56. with Runner() as runner:
  57. runner.run(echo("World"))
  58. # Hello World
  59. ```
  60. ## uvloop
  61. This backport is also compatible with [uvloop](https://github.com/MagicStack/uvloop). An example is below:
  62. ```python
  63. import sys
  64. import uvloop
  65. if sys.version_info < (3, 11):
  66. from backports.asyncio.runner import Runner
  67. else:
  68. from asyncio import Runner
  69. async def echo(msg: str) -> None:
  70. print(f"Hello {msg}")
  71. with Runner(loop_factory=uvloop.new_event_loop) as runner:
  72. runner.run(echo("World"))
  73. # Hello World
  74. ```
  75. ## Contributing
  76. Feel free to open a PR if there's changes you'd like to make as long as the changes maintain full reference implementation
  77. with Python 3.11's implementation of `asyncio.Runner`. More documentation and additional tests are always welcome as CPython's
  78. test don't seem to provide 100% coverage at a glance or more likely I may have missed including some tests.
  79. * [ruff](https://github.com/astral-sh/ruff) is used to format the code via `ruff format`.
  80. * To run ruff, make sure `uv` is installed and run `uv run ruff format src tests`
  81. * To run ruff on applicable python versions, run `uv run nox -rs fmt`
  82. * [mypy](https://github.com/python/mypy) is used to check types in the sources (while attempting to stay true to reference implementation).
  83. * To run mypy, make sure `uv` is installed and run `uv run mypy src`
  84. * To run mypy on applicable python versions, run `uv run nox -rs ty`
  85. * To run tests use `uv run python -m unittest discover -s tests`
  86. * For applicable python versions `uv run nox -rs tests`
  87. * To gather coverage use `uv run coverage run -m unittest discover -s tests`
  88. * For applicable python versions `uv run nox -rs tests -- --cov`
  89. Relevant reference implementation sources:
  90. * https://github.com/python/cpython/blob/3.11/Lib/asyncio/runners.py
  91. * https://github.com/python/cpython/blob/3.11/Lib/asyncio/tasks.py
  92. * https://github.com/python/cpython/blob/3.11/Lib/asyncio/base_events.py
  93. * https://github.com/python/cpython/blob/3.11/Modules/_asynciomodule.c
  94. * https://github.com/python/cpython/blob/3.11/Lib/test/test_asyncio/test_runners.py
  95. * https://github.com/python/cpython/blob/3.8/Lib/test/test_asyncio/test_tasks.py
  96. * https://github.com/python/cpython/blob/3.9/Lib/test/test_asyncio/test_tasks.py
  97. * https://github.com/python/cpython/blob/3.10/Lib/test/test_asyncio/test_tasks.py
  98. ## Caveats
  99. This implementation uses `asyncio.tasks._PyTask` instead of `asyncio.tasks._CTask` as it adds additional functionality to
  100. `asyncio.Task` in order to support `asyncio.Runner` requirements. Hence, the `asyncio.Task` implementation `Runner` will
  101. use also comes from this package. As such, problems can arise when checking `isinstance(some_runner_main_task, asyncio.Task)`
  102. since `asyncio.Task` can point to `_CTask` by default instead of `_PyTask`. You may encounter the same issue when comparing
  103. it to `asyncio.Future`.
  104. To guarantee full `asyncio.Task` functionality, upstream tests for `asyncio.Task` for 3.8, 3.9, and 3.10 are also part of the
  105. testing suite of this package to make sure all changes to `asyncio.Task` used by the `Runner` are fully compatible with
  106. `asyncio.Task`. Important Note: `asyncio.Task` is **only** patched on the `Runner`, not globally, hence there should be
  107. no side effects to external code when using this module.
  108. Currently, a backport of `_CTask` is not provided on this package and not in scope at this time. This means that there's a slight
  109. performance degradation when using this `asyncio.Runner` implementation over the one in Python 3.11 or above.
  110. # 1.2.0
  111. * Backport bug fix for https://github.com/python/cpython/issues/112559
  112. * Declare `patch` and `patch.patch_object` as internal.
  113. * Improved `runner.py` typedefs.
  114. # 1.1.0
  115. * Add basic `noxfile` with uv backend.
  116. * Simplified patching implementation.
  117. * Test dependency updates.
  118. # 1.0.2
  119. * Add `uvloop` example to README.md
  120. * Improved tests coverage.
  121. * Improved modified code documentation.
  122. # 1.0.1
  123. * `requires-python` metadata is now properly set to ">=3.8,<3.11"
  124. # 1.0.0
  125. * Initial Reference Implementation