[Python-ideas] async objects
Vincent Michel
vxgmichel at gmail.com
Thu Oct 6 11:55:48 EDT 2016
2016-10-06 13:50 GMT+02:00 Nick Coghlan <ncoghlan at gmail.com>:
> The shadow thread idea will hopefully prove successful in addressing
> the last major rough spot in the UX, which is the ability to easily
> integrate asynchronous components into an otherwise synchronous
> application.
>
That's my opinion as well. If I had to run asyncio coroutines from
synchronous code, I'd probably take advantage of the Executor interface
defined by concurrent.futures. Executors handle resource management through
a context manager interface, which is a good way to start and clean after
the shadow thread. Also, the submit method returns a
concurrent.futures.Future, i.e. the standard for accessing an asynchronous
result from synchronous code. Here's a simple implementation:
https://gist.github.com/vxgmichel/d16e66d1107a369877f6ef7e646ac2e5
If this is not enough, (say one wants to write a synchronous API to an
asynchronous library), then it simply is a matter of instantiating the
executor once in the module and wrap all the coroutines to expose with
executor.submit and Future.result. This might provide an acceptable answer
to the DRY thing that has been mentioned a few times, though I'm not
convinced it is such a problematic issue (at least nothing that sans-io
already addresses in the first place).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161006/343cf77e/attachment-0001.html>
More information about the Python-ideas
mailing list