[Python-ideas] How the heck does async/await work in Python 3.5
Gregory Ewing
greg.ewing at canterbury.ac.nz
Wed Feb 24 16:00:29 EST 2016
王珺 wrote:
> Suppose io_operation() takes 3 seconds, then how can I write something like
>
> future = io_operation()
> print('Start')
> time.sleep(1)
> print('Something')
> time.sleep(2)
> print(future.result())
>
> that print 'Start' immediately and the result of io_operation() 3 seconds
> later.
Yes, Python can do this, but you probably need to use real
threads. The only exception would be if io_operation() were
something you could fire off with a single system call that's
guaranteed not to block, and then wait for the result later.
Opportunities for things like that are rare in unix.
(Windows might be different, I'm not sure.)
--
Greg
More information about the Python-list
mailing list