66 PyAwaitable documentation
77=========================
88
9- Add your content using ``reStructuredText `` syntax. See the
10- `reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html >`_
11- documentation for details.
12-
13-
149.. toctree ::
1510 :maxdepth: 2
1611 :caption: Contents:
@@ -20,3 +15,86 @@ documentation for details.
2015 usage/adding_awaits
2116 usage/value_storage
2217 reference
18+
19+ .. note ::
20+
21+ This project originates from a scrapped PEP. For the original text,
22+ see `here <https://gist.github.com/ZeroIntensity/8d32e94b243529c7e1c27349e972d926 >`_.
23+
24+ Introduction
25+ ------------
26+
27+ CPython currently has no existing C interface for writing asynchronous functions
28+ or doing any sort of :keyword: `await ` operations, other than defining
29+ extension types and manually implementing methods like
30+ :attr: `~object.__await__ ` from scratch. This lack of an API can be seen in
31+ some Python-to-C transpilers (such as ``mypyc ``) having limited support for
32+ asynchronous code.
33+
34+ In the C API, developers are forced to do one of three things when it comes
35+ to asynchronous code:
36+
37+ - Manually implementing coroutines using extension types.
38+ - Use an external tool to compile their asynchronous code to C.
39+ - Defer their asynchronous logic to a synchronous Python function, and then
40+ call that natively.
41+
42+ Since there are other event loop implementations, PyAwaitable aims to be a
43+ generic interface for working with asynchronous operations from C (as in,
44+ we'll only be implementing features like ``async def `` and :keyword: `await `,
45+ but not things like :func: `asyncio.create_task `).
46+
47+ This documentation assumes that you're familiar with the C API already,
48+ and understand some essential concepts like reference counting (as well as
49+ borrowed and strong references). If you don't know what any of that means, it's
50+ highly advised that you read through the
51+ :ref: `Python documentation <extending-intro >` before trying to use
52+ PyAwaitable.
53+
54+ Quickstart
55+ ----------
56+
57+ Add PyAwaitable as a build dependency:
58+
59+ .. code-block :: toml
60+
61+ # pyproject.toml
62+ [build-system]
63+ requires = ["your_preferred_build_system", "pyawaitable>=2.0.0"]
64+ build-backend = "your_preferred_build_system.build"
65+
66+ Use it in your extension:
67+
68+ .. code-block :: c
69+
70+ /*
71+ Equivalent to the following Python function:
72+
73+ async def async_function(coro: collections.abc.Awaitable) -> None:
74+ await coro
75+ */
76+ static PyObject *
77+ async_function(PyObject *self, PyObject *coro)
78+ {
79+ PyObject *awaitable = PyAwaitable_New();
80+ if (awaitable == NULL) {
81+ return NULL;
82+ }
83+
84+ if (PyAwaitable_AddAwait(awaitable, coro, NULL, NULL) < 0) {
85+ Py_DECREF(awaitable);
86+ return NULL;
87+ }
88+
89+ return awaitable;
90+ }
91+
92+
93+ Acknowledgements
94+ ----------------
95+
96+ Special thanks to:
97+
98+ - `Petr Viktorin <https://github.com/encukou >`_, for his feedback on the
99+ initial API design and PEP.
100+ - `Sean Hunt <https://github.com/AraHaan >`_, for beta testing.
0 commit comments