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.
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: Alyssa 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: Alyssa 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:18 | admin | set | github: 68766 |
| 2015年07月07日 13:13:10 | ncoghlan | set | status: open -> closed superseder: [RFE] Add asyncio.background_call API resolution: duplicate messages: + msg246412 |
| 2015年07月07日 13:05:32 | gvanrossum | set | messages: + msg246410 |
| 2015年07月07日 12:32:10 | ncoghlan | set | messages: + msg246406 |
| 2015年07月06日 23:41:14 | martin.panter | set | nosy:
+ martin.panter messages: + msg246383 |
| 2015年07月06日 18:03:13 | srkunze | set | nosy:
+ srkunze |
| 2015年07月06日 18:02:58 | srkunze | set | nosy:
+ ncoghlan, pitrou, giampaolo.rodola, - srkunze type: enhancement |
| 2015年07月06日 18:01:53 | srkunze | create | |