This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [RFE] Add asyncio.wait_for_result API
Type: enhancement Stage:
Components: asyncio Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: [RFE] Add asyncio.background_call API
View: 24571
Assigned To: Nosy List: giampaolo.rodola, gvanrossum, martin.panter, ncoghlan, pitrou, srkunze, vstinner, yselivanov
Priority: normal Keywords:

Created on 2015-07-06 18:01 by srkunze, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg246373 - (view) Author: Sven R. Kunze (srkunze) Date: 2015-07-06 18:01
In order to complement http://bugs.python.org/issue24571, this is another high-level convenience API for asyncio to treat an awaitable like a usual subroutine (credits go to Nick Coghlan):

    # Call awaitable from synchronous code
    def wait_for_result(awaitable):
        """Usage: result = asyncio.wait_for_result(awaitable)"""
        return asyncio.get_event_loop().run_until_complete(awaitable.__await__())

It may not be that conceptually dense, however, I feel for projects transitioning from the classical subroutine world to the asyncio world, this functionality might prove useful to bridge both worlds seamlessly when necessary.
msg246383 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-07-06 23:41
I don’t think you need the __await__() call. Just do loop.run_until_complete(awaitable).

I understand “asyncio” doesn’t support recursive calls into the same event loop on purpose; see Issue 22239. So this code would only be useful from outside of the event loop, or if more than one event loop was in use.
msg246406 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-07-07 12:32
It occurs to me that given both this API and the "call_async()" API now proposed in issue 24571, otherwise synchronous code can do things like:

    futureB = asyncio.call_async(slow_io_bound_operation)
    futureC = asyncio.call_async(another_slow_io_bound_operation)
    a = calculateA()
    b = asyncio.wait_for_result(futureB)
    c = asyncio.wait_for_result(futureC)

Which still reads well when combined with await:

    b = await asyncio.call_async(blocking_operation)
msg246410 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2015-07-07 13:05
Maybe the two issues should be merged so the two proposals can be considered together. I'm -0 on both, because each of these is really just one line of code and it seems they both encourage mindless copy-pasting that just saddens me (similar to "Python" scripts I sometimes see that are just a series of shell invocations using subprocess.getoutput() or similar, including calls to "rm" or "echo").
msg246412 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-07-07 13:13
As Guido suggested, merging back into issue 24571
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68766
2015-07-07 13:13:10ncoghlansetstatus: open -> closed
superseder: [RFE] Add asyncio.background_call API
resolution: duplicate
messages: + msg246412
2015-07-07 13:05:32gvanrossumsetmessages: + msg246410
2015-07-07 12:32:10ncoghlansetmessages: + msg246406
2015-07-06 23:41:14martin.pantersetnosy: + martin.panter
messages: + msg246383
2015-07-06 18:03:13srkunzesetnosy: + srkunze
2015-07-06 18:02:58srkunzesetnosy: + ncoghlan, pitrou, giampaolo.rodola, - srkunze
type: enhancement
2015-07-06 18:01:53srkunzecreate