spinner example ported to async/await and curio
This commit is contained in:
parent
d7e37ad0fd
commit
8bbaeb1e36
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
*.sublime-project
|
||||
*.sublime-workspace
|
||||
.env*
|
||||
concurrency/flags/img/*.gif
|
||||
concurrency/charfinder/charfinder_index.pickle
|
||||
18-asyncio/charfinder/charfinder_index.pickle
|
||||
|
2
18-asyncio/spinner_asyncio.py
Normal file → Executable file
2
18-asyncio/spinner_asyncio.py
Normal file → Executable file
@ -1,3 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# spinner_asyncio.py
|
||||
|
||||
# credits: Example by Luciano Ramalho inspired by
|
||||
|
50
18-asyncio/spinner_await.py
Executable file
50
18-asyncio/spinner_await.py
Executable file
@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# spinner_await.py
|
||||
|
||||
# credits: Example by Luciano Ramalho inspired by
|
||||
# Michele Simionato's multiprocessing example in the python-list:
|
||||
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
|
||||
|
||||
import asyncio
|
||||
import itertools
|
||||
import sys
|
||||
|
||||
|
||||
async def spin(msg): # <1>
|
||||
write, flush = sys.stdout.write, sys.stdout.flush
|
||||
for char in itertools.cycle('|/-\\'):
|
||||
status = char + ' ' + msg
|
||||
write(status)
|
||||
flush()
|
||||
write('\x08' * len(status))
|
||||
try:
|
||||
await asyncio.sleep(.1) # <2>
|
||||
except asyncio.CancelledError: # <3>
|
||||
break
|
||||
write(' ' * len(status) + '\x08' * len(status))
|
||||
|
||||
|
||||
async def slow_function(): # <4>
|
||||
# pretend waiting a long time for I/O
|
||||
await asyncio.sleep(3) # <5>
|
||||
return 42
|
||||
|
||||
|
||||
async def supervisor(): # <6>
|
||||
spinner = asyncio.ensure_future(spin('thinking!')) # <7>
|
||||
print('spinner object:', spinner) # <8>
|
||||
result = await slow_function() # <9>
|
||||
spinner.cancel() # <10>
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
loop = asyncio.get_event_loop() # <11>
|
||||
result = loop.run_until_complete(supervisor()) # <12>
|
||||
loop.close()
|
||||
print('Answer:', result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
49
18-asyncio/spinner_curio.py
Executable file
49
18-asyncio/spinner_curio.py
Executable file
@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# spinner_curio.py
|
||||
|
||||
# credits: Example by Luciano Ramalho inspired by
|
||||
# Michele Simionato's multiprocessing example in the python-list:
|
||||
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
|
||||
|
||||
import curio
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
|
||||
|
||||
async def spin(msg): # <1>
|
||||
write, flush = sys.stdout.write, sys.stdout.flush
|
||||
for char in itertools.cycle('|/-\\'):
|
||||
status = char + ' ' + msg
|
||||
write(status)
|
||||
flush()
|
||||
write('\x08' * len(status))
|
||||
try:
|
||||
await curio.sleep(.1) # <2>
|
||||
except curio.CancelledError: # <3>
|
||||
break
|
||||
write(' ' * len(status) + '\x08' * len(status))
|
||||
|
||||
|
||||
async def slow_function(): # <4>
|
||||
# pretend waiting a long time for I/O
|
||||
await curio.sleep(3) # <5>
|
||||
return 42
|
||||
|
||||
|
||||
async def supervisor(): # <6>
|
||||
spinner = await curio.spawn(spin('thinking!')) # <7>
|
||||
print('spinner object:\n ', repr(spinner)) # <8>
|
||||
result = await slow_function() # <9>
|
||||
await spinner.cancel() # <10>
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
result = curio.run(supervisor) # <12>
|
||||
print('Answer:', result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
2
18-asyncio/spinner_thread.py
Normal file → Executable file
2
18-asyncio/spinner_thread.py
Normal file → Executable file
@ -1,3 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# spinner_thread.py
|
||||
|
||||
# credits: Adapted from Michele Simionato's
|
||||
|
Loading…
Reference in New Issue
Block a user