[Python-Dev] Importance of "async" keyword
Nick Coghlan
ncoghlan at gmail.com
Tue Jul 7 14:40:37 CEST 2015
On 7 July 2015 at 06:08, Sven R. Kunze <srkunze at mail.de> wrote:
> I would like to rewrite/amend it to work asynchronously with minimal effort
> such as:
>
> def business_new():
> content1 = fork open('big.file').read() # wraps up the calls into
> awaitables
> content2 = fork open('huge.file').read() # and put them into the event
> loop
> return content1 + content2 # variables are used => await
> evaluation
>
> I might have missed something but I think you get my point.
No, you haven't missed anything, but I think the convenience APIs
we're discussing in this thread are what you need, rather than
async/await.
Specifically, your main application would remain synchronous, but the
event loop could be used to run selected operations in a background
thread:
def business_new():
future1 = asyncio.call_async(open('big.file').read)
future2 = asyncio.call_async(open('huge.file').read)
content1 = asyncio.wait_for_result(future1)
content2 = asyncio.wait_for_result(future2)
return content1 + content2
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev
mailing list