pymport
pymport copied to clipboard
Background Python operations can block the event loop
Consider the following scenario:
const q = pyCallable.callAsync(pyArg);
const b = somePyObject.attribute;
await q;
The first operation launches a Python operation in a background thread (using an existing thread in libuv
's pool).
Python however is a single-threaded interpreter with a limited shared-memory multi-threading support - but not one which involves actually simultaneously interpreting Python code. Be sure to read Async
in the wiki.
This means that the second line will effectively block the event loop until the first operation concludes.
A small, but notable exception to this rule is native Python extensions like numpy
which release the Python GIL while running C code.
No Python object can be accessed while a Python background operation is running.
This is a fundamental Python limitation that is unlikely to go away in a foreseeable future.