Sujet : Re: Using a background thread with asyncio/futures with flask
De : dieter.maurer (at) *nospam* online.de
Groupes : comp.lang.pythonDate : 22. Mar 2024, 21:28:31
Autres entêtes
Message-ID : <mailman.17.1711136505.3468.python-list@python.org>
References : 1 2 3 4 5
User-Agent : VM 8.0.12-devo-585 under 21.4 (patch 24) "Standard C" XEmacs Lucid (x86_64-linux-gnu)
dieter.maurer@online.de wrote at 2024-3-22 18:28 +0100:
Thomas Nyberg wrote at 2024-3-22 11:08 +0100:
... `future` use across thread boundaries ...
Here's an example using just the standard library that
exhibits the same issue:
...
For use across thread boundaries, you likely will use
`concurrent.Future` (not `asyncio.Future`).
You can use `asyncio.futures._chain_futures` to associate
an `asyncio.Future` with a `concurrent.Future`.
Then the fate (result or exception set) of one will be reflected in the other.
You must not set the result/exception for a future in a "foreign" thread
("foreign" here means one not associated with the future's loop).
An aternative to the solution sketched above is to set the result
indirectly via `loop.call_soon_threadsafe`. This way, the
result is set in the futures "native" thread.