updated some examples from ch. 17 e 18 to Python 3.7
This commit is contained in:
parent
eeb6b3d498
commit
29d6835f3b
@ -31,7 +31,7 @@ async def slow_function(): # <5>
|
||||
|
||||
|
||||
async def supervisor(): # <7>
|
||||
spinner = asyncio.ensure_future(spin('thinking!')) # <8>
|
||||
spinner = asyncio.create_task(spin('thinking!')) # <8>
|
||||
print('spinner object:', spinner) # <9>
|
||||
result = await slow_function() # <10>
|
||||
spinner.cancel() # <11>
|
||||
@ -39,9 +39,7 @@ async def supervisor(): # <7>
|
||||
|
||||
|
||||
def main():
|
||||
loop = asyncio.get_event_loop() # <12>
|
||||
result = loop.run_until_complete(supervisor()) # <13>
|
||||
loop.close()
|
||||
result = asyncio.run(supervisor()) # <12>
|
||||
print('Answer:', result)
|
||||
|
||||
|
@ -1,19 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Charfinder</title>
|
||||
</head>
|
||||
<body>
|
||||
Examples: {links}
|
||||
<p>
|
||||
<form action="/">
|
||||
<input type="search" name="query" value="{query}">
|
||||
<input type="submit" value="find"> {message}
|
||||
</form>
|
||||
</p>
|
||||
<table>
|
||||
{result}
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
@ -1,71 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import asyncio
|
||||
from aiohttp import web
|
||||
|
||||
from charfinder import UnicodeNameIndex
|
||||
|
||||
TEMPLATE_NAME = 'http_charfinder.html'
|
||||
CONTENT_TYPE = 'text/html; charset=UTF-8'
|
||||
SAMPLE_WORDS = ('bismillah chess cat circled Malayalam digit'
|
||||
' Roman face Ethiopic black mark symbol dot'
|
||||
' operator Braille hexagram').split()
|
||||
|
||||
ROW_TPL = '<tr><td>{code_str}</td><th>{char}</th><td>{name}</td></tr>'
|
||||
LINK_TPL = '<a href="/?query={0}" title="find "{0}"">{0}</a>'
|
||||
LINKS_HTML = ', '.join(LINK_TPL.format(word) for word in
|
||||
sorted(SAMPLE_WORDS, key=str.upper))
|
||||
|
||||
|
||||
index = UnicodeNameIndex()
|
||||
with open(TEMPLATE_NAME) as tpl:
|
||||
template = tpl.read()
|
||||
template = template.replace('{links}', LINKS_HTML)
|
||||
|
||||
# BEGIN HTTP_CHARFINDER_HOME
|
||||
def home(request): # <1>
|
||||
query = request.GET.get('query', '').strip() # <2>
|
||||
print('Query: {!r}'.format(query)) # <3>
|
||||
if query: # <4>
|
||||
descriptions = list(index.find_descriptions(query))
|
||||
res = '\n'.join(ROW_TPL.format(**vars(descr))
|
||||
for descr in descriptions)
|
||||
msg = index.status(query, len(descriptions))
|
||||
else:
|
||||
descriptions = []
|
||||
res = ''
|
||||
msg = 'Enter words describing characters.'
|
||||
|
||||
html = template.format(query=query, result=res, # <5>
|
||||
message=msg)
|
||||
print('Sending {} results'.format(len(descriptions))) # <6>
|
||||
return web.Response(content_type=CONTENT_TYPE, text=html) # <7>
|
||||
# END HTTP_CHARFINDER_HOME
|
||||
|
||||
|
||||
# BEGIN HTTP_CHARFINDER_SETUP
|
||||
async def init(loop, address, port): # <1>
|
||||
app = web.Application(loop=loop) # <2>
|
||||
app.router.add_route('GET', '/', home) # <3>
|
||||
handler = app.make_handler() # <4>
|
||||
server = await loop.create_server(handler,
|
||||
address, port) # <5>
|
||||
return server.sockets[0].getsockname() # <6>
|
||||
|
||||
def main(address="127.0.0.1", port=8888):
|
||||
port = int(port)
|
||||
loop = asyncio.get_event_loop()
|
||||
host = loop.run_until_complete(init(loop, address, port)) # <7>
|
||||
print('Serving on {}. Hit CTRL-C to stop.'.format(host))
|
||||
try:
|
||||
loop.run_forever() # <8>
|
||||
except KeyboardInterrupt: # CTRL+C pressed
|
||||
pass
|
||||
print('Server shutting down.')
|
||||
loop.close() # <9>
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(*sys.argv[1:])
|
||||
# END HTTP_CHARFINDER_SETUP
|
@ -1,53 +0,0 @@
|
||||
# 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 # <1>
|
||||
def spin(msg): # <2>
|
||||
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) # <3>
|
||||
except asyncio.CancelledError: # <4>
|
||||
break
|
||||
write(' ' * len(status) + '\x08' * len(status))
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def slow_function(): # <5>
|
||||
# pretend waiting a long time for I/O
|
||||
yield from asyncio.sleep(3) # <6>
|
||||
return 42
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def supervisor(): # <7>
|
||||
spinner = asyncio.async(spin('thinking!')) # <8>
|
||||
print('spinner object:', spinner) # <9>
|
||||
result = yield from slow_function() # <10>
|
||||
spinner.cancel() # <11>
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
loop = asyncio.get_event_loop() # <12>
|
||||
result = loop.run_until_complete(supervisor()) # <13>
|
||||
loop.close()
|
||||
print('Answer:', result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
# END SPINNER_ASYNCIO
|
Loading…
x
Reference in New Issue
Block a user