You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Provide a Boost.Asio-based implementation for the Python [@https://docs.python.org/3/library/asyncio-eventloop.html `EventLoop`] type. Every callback is scheduled in strand.
[[Effect][construct an `event_loop` object using provided [@https://www.boost.org/doc/libs/1_76_0/doc/html/boost_asio/overview/core/strands.html `strand`] object. Setup a new [@https://docs.python.org/3/library/asyncio-policy.html event loop policy], when user call `get_event_loop` using that policy, it returns the Boost Asio `event_loop` object]]
17
+
[[Throws][nothing]]
18
+
]
19
+
[endsect]
20
+
21
+
[section Function `PyInit_boost_event_loop`]
22
+
``
23
+
extern "C"
24
+
{
25
+
PyObject* PyInit_boost_event_loop();
26
+
}
27
+
``
28
+
[variablelist
29
+
[[Effect][user must call `PyImport_AppendInittab("boost_event_loop", &PyInit_boost_event_loop);` before [@https://docs.python.org/3/c-api/init.html#c.Py_Initialize `Py_Initialize`]]]
[section Event Loop and Multiple Python Sub-interpreters]
60
+
It's allowed to have multiple Python sub-interpreter instances in a same program. Each interpreter will act as a guest VM, and C++ host will schedule all the asynchronous events committed by the Python VM.
61
+
[endsect]
62
+
63
+
[section Create, Swap, and Destroy Sub-interpreter]
This will create an interpreter and its first thread.[br]
69
+
Call [@https://docs.python.org/3/c-api/init.html#c.PyThreadState_Swap `PyThreadState_Swap`] to swap between different interpreter.[br]
70
+
To destroy an interpreter, simply call
71
+
``
72
+
// this will destroy the interpreter and switch to other_ts
73
+
Py_EndInterpreter(ts);
74
+
PyThreadState_Swap(other_ts);
75
+
``
76
+
The Python interpreter must outlive the [@https://www.boost.org/doc/libs/1_76_0/doc/html/boost_asio/reference/io_service.html `asio::io_context`] objects it owns. It's not safe to destroy the interpreter midways.[br]
77
+
[@https://docs.python.org/3/c-api/structures.html#c.PyObject `PyObject`] [@https://docs.python.org/3/c-api/refcounting.html `reference count`] won't protect its interpreter from being destroyed.
GIL is shared by all sub-interpreters in the same program, which means after setting up the Python IO object and call the async functions, C++ host should release the GIL of current interpreter.
0 commit comments