update from atlas

This commit is contained in:
Luciano Ramalho
2015-03-22 17:01:13 -03:00
parent 8fb691d3c7
commit 290079ec9a
16 changed files with 803 additions and 2 deletions

View File

@@ -0,0 +1,53 @@
# spinner_asyncio.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
# BEGIN SPINNER_ASYNCIO
import asyncio
import itertools
import sys
@asyncio.coroutine
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:
yield from asyncio.sleep(.1) # <2>
except asyncio.CancelledError: # <3>
break
write(' ' * len(status) + '\x08' * len(status))
@asyncio.coroutine
def slow_computation(): # <4>
# fake computation waiting a long time for I/O
yield from asyncio.sleep(3) # <5>
return 42
@asyncio.coroutine
def supervisor(): # <6>
spinner = asyncio.async(spin('thinking!')) # <7>
print('spinner object:', spinner) # <8>
result = yield from slow_computation() # <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()
# END SPINNER_ASYNCIO

View File

@@ -0,0 +1,56 @@
# spinner_thread.py
# credits: Adapted from Michele Simionato's
# multiprocessing example in the python-list:
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
# BEGIN SPINNER_THREAD
import threading
import itertools
import time
import sys
class Signal: # <1>
go = True
def spin(msg, signal): # <2>
write, flush = sys.stdout.write, sys.stdout.flush
for char in itertools.cycle('|/-\\'): # <3>
status = char + ' ' + msg
write(status)
flush()
write('\x08' * len(status)) # <4>
time.sleep(.1)
if not signal.go: # <5>
break
write(' ' * len(status) + '\x08' * len(status)) # <6>
def slow_computation(): # <7>
# fake computation waiting a long time for I/O
time.sleep(3) # <8>
return 42
def supervisor(): # <9>
signal = Signal()
spinner = threading.Thread(None, spin,
args=('thinking!', signal))
print('spinner object:', spinner) # <10>
spinner.start() # <11>
result = slow_computation() # <12>
signal.go = False # <13>
spinner.join() # <14>
return result
def main():
result = supervisor() # <15>
print('Answer:', result)
if __name__ == '__main__':
main()
# END SPINNER_THREAD