Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

Promote the use of a main coroutine #13

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# html_static_path = ['_static']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
Expand Down
47 changes: 20 additions & 27 deletions examples/aiohttp_client.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
"""aiohttp-based client to retrieve web pages.
"""
"""aiohttp-based client to retrieve web pages."""

import asyncio
from contextlib import closing
import time

import asyncio
import aiohttp


async def fetch_page(session, host, port=8000, wait=0):
"""Get one page.
"""
"""Get one page."""
url = '{}:{}/{}'.format(host, port, wait)
with aiohttp.Timeout(10):
async with session.get(url) as response:
assert response.status == 200
return await response.text()
text = await response.text()
return text.strip('\n')


def get_multiple_pages(host, waits, port=8000, show_time=True):
"""Get multiple pages.
"""
tasks = []
pages = []
async def get_multiple_pages(host, waits, port=8000, show_time=True):
"""Get multiple pages."""
start = time.perf_counter()
with closing(asyncio.get_event_loop()) as loop:
with aiohttp.ClientSession(loop=loop) as session:
for wait in waits:
tasks.append(fetch_page(session, host, port, wait))
pages = loop.run_until_complete(asyncio.gather(*tasks))
with aiohttp.ClientSession() as session:
tasks = [fetch_page(session, host, port, wait) for wait in waits]
pages = await asyncio.gather(*tasks)
duration = time.perf_counter() - start
sum_waits = sum(waits)
if show_time:
Expand All @@ -37,14 +29,15 @@ def get_multiple_pages(host, waits, port=8000, show_time=True):
return pages


if __name__ == '__main__':
async def main():
"""Test it."""
pages = await get_multiple_pages(
host='http://localhost', port='8000', waits=[1, 5, 3, 2])
for page in pages:
print(page)

def main():
"""Test it.
"""
pages = get_multiple_pages(host='http://localhost', port='8000',
waits=[1, 5, 3, 2])
for page in pages:
print(page)

main()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
38 changes: 18 additions & 20 deletions examples/async_client_blocking.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
"""Get "web pages.
"""Get web pages.

Waiting until one pages is download before getting the next."
Waiting until one pages is download before getting the next.
"""

import asyncio
from contextlib import closing
import time

import asyncio
from async_page import get_page


def get_multiple_pages(host, port, waits, show_time=True):
"""Get multiple pages.
"""
async def get_multiple_pages(host, port, waits, show_time=True):
"""Get multiple pages."""
start = time.perf_counter()
pages = []
with closing(asyncio.get_event_loop()) as loop:
for wait in waits:
pages.append(loop.run_until_complete(get_page(host, port, wait)))
for wait in waits:
pages.append(await get_page(host, port, wait))
duration = time.perf_counter() - start
sum_waits = sum(waits)
if show_time:
msg = 'It took {:4.2f} seconds for a total waiting time of {:4.2f}.'
print(msg.format(duration, sum_waits))
return pages

if __name__ == '__main__':

def main():
"""Test it.
"""
pages = get_multiple_pages(host='localhost', port='8000',
waits=[1, 5, 3, 2])
for page in pages:
print(page)
async def main():
"""Test it."""
pages = await get_multiple_pages(
host='localhost', port='8000', waits=[1, 5, 3, 2])
for page in pages:
print(page)


main()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
41 changes: 18 additions & 23 deletions examples/async_client_nonblocking.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
"""Get "web pages.
"""Get web pages.

Waiting until one pages is download before getting the next."
Waiting until one pages is download before getting the next.
"""

import asyncio
from contextlib import closing
import time

import asyncio
from async_page import get_page


def get_multiple_pages(host, port, waits, show_time=True):
"""Get multiple pages.
"""
async def get_multiple_pages(host, port, waits, show_time=True):
"""Get multiple pages."""
start = time.perf_counter()
pages = []
tasks = []
with closing(asyncio.get_event_loop()) as loop:
for wait in waits:
tasks.append(get_page(host, port, wait))
pages = loop.run_until_complete(asyncio.gather(*tasks))
tasks = [get_page(host, port, wait) for wait in waits]
pages = await asyncio.gather(*tasks)
duration = time.perf_counter() - start
sum_waits = sum(waits)
if show_time:
msg = 'It took {:4.2f} seconds for a total waiting time of {:4.2f}.'
print(msg.format(duration, sum_waits))
return pages

if __name__ == '__main__':

def main():
"""Test it.
"""
pages = get_multiple_pages(host='localhost', port='8000',
waits=[1, 5, 3, 2])
for page in pages:
print(page)
async def main():
"""Test it."""
pages = await get_multiple_pages(
host='localhost', port='8000', waits=[1, 5, 3, 2])
for page in pages:
print(page)


main()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
11 changes: 3 additions & 8 deletions examples/async_page.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# file: async_page.py

"""Get a "web page" asynchronously.
"""
"""Get a web page asynchronously."""

import asyncio

ENCODING = 'ISO-8859-1'


def get_encoding(header):
"""Find out encoding.
"""
"""Find out encoding."""
for line in header:
if line.lstrip().startswith('Content-type'):
for entry in line.split(';'):
Expand All @@ -20,8 +16,7 @@ def get_encoding(header):


async def get_page(host, port, wait=0):
"""Get a "web page" asynchronously.
"""
"""Get a web page asynchronously."""
reader, writer = await asyncio.open_connection(host, port)
writer.write(b'\r\n'.join([
'GET /{} HTTP/1.0'.format(wait).encode(ENCODING),
Expand Down
4 changes: 1 addition & 3 deletions examples/asyncio_deferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ async def steps(x):


loop = asyncio.get_event_loop()
coro = steps(5)
loop.run_until_complete(coro)
loop.run_until_complete(steps(5))
loop.close()

9 changes: 6 additions & 3 deletions examples/create_task.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import asyncio


async def say(what, when):
await asyncio.sleep(when)
print(what)


loop = asyncio.get_event_loop()
async def schedule():
asyncio.ensure_future(say('first hello', 2))
asyncio.ensure_future(say('second hello', 1))

loop.create_task(say('first hello', 2))
loop.create_task(say('second hello', 1))

loop = asyncio.get_event_loop()
loop.run_until_complete(schedule())
loop.run_forever()
loop.close()
12 changes: 7 additions & 5 deletions examples/hello_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


async def print_every_second():
"Print seconds"
while True:
for i in range(60):
print(i, 's')
Expand All @@ -15,9 +14,12 @@ async def print_every_minute():
print(i, 'minute')


async def main():
coro_second = print_every_second()
coro_minute = print_every_minute()
await asyncio.gather(coro_second, coro_minute)


loop = asyncio.get_event_loop()
loop.run_until_complete(
asyncio.gather(print_every_second(),
print_every_minute())
)
loop.run_until_complete(main())
loop.close()
2 changes: 2 additions & 0 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio


async def say(what, when):
await asyncio.sleep(when)
print(what)


loop = asyncio.get_event_loop()
loop.run_until_complete(say('hello world', 1))
loop.close()
13 changes: 9 additions & 4 deletions examples/http_client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import asyncio
import aiohttp


async def fetch_page(session, url):
with aiohttp.Timeout(10):
async with session.get(url) as response:
assert response.status == 200
return await response.read()


async def main():
with aiohttp.ClientSession() as session:
content = await fetch_page(session, 'http://python.org')
print(content.decode())


loop = asyncio.get_event_loop()
with aiohttp.ClientSession(loop=loop) as session:
content = loop.run_until_complete(
fetch_page(session, 'http://python.org'))
print(content)
loop.run_until_complete(main())
loop.close()
18 changes: 11 additions & 7 deletions examples/loop_stop.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import asyncio


async def say(what, when):
await asyncio.sleep(when)
print(what)

async def stop_after(loop, when):

async def stop_after(when):
await asyncio.sleep(when)
loop.stop()
asyncio.get_event_loop().stop()


loop = asyncio.get_event_loop()
async def schedule():
asyncio.ensure_future(say('first hello', 2))
asyncio.ensure_future(say('second hello', 1))
asyncio.ensure_future(say('third hello', 4))
asyncio.ensure_future(stop_after(3))

loop.create_task(say('first hello', 2))
loop.create_task(say('second hello', 1))
loop.create_task(say('third hello', 4))
loop.create_task(stop_after(loop, 3))

loop = asyncio.get_event_loop()
loop.run_until_complete(schedule())
loop.run_forever()
loop.close()
29 changes: 17 additions & 12 deletions examples/producer_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@


async def produce(queue, n):
for x in range(1, n + 1):
# produce an item
# produce n items
for x in range(1, n+1):

# produce an item (simulate i/o operation using sleep)
print('producing {}/{}'.format(x, n))
# simulate i/o operation using sleep
await asyncio.sleep(random.random())
item = str(x)
item = await asyncio.sleep(random.random(), result=x)

# put the item in the queue
await queue.put(item)

Expand All @@ -20,19 +21,23 @@ async def consume(queue):
while True:
# wait for an item from the producer
item = await queue.get()

# the producer emits None to indicate that it is done
if item is None:
# the producer emits None to indicate that it is done
break

# process the item
# process the item (simulate i/o operation using sleep)
print('consuming item {}...'.format(item))
# simulate i/o operation using sleep
await asyncio.sleep(random.random())


async def main():
queue = asyncio.Queue()
producer_coro = produce(queue, 10)
consumer_coro = consume(queue)
await asyncio.gather(producer_coro, consumer_coro)


loop = asyncio.get_event_loop()
queue = asyncio.Queue(loop=loop)
producer_coro = produce(queue, 10)
consumer_coro = consume(queue)
loop.run_until_complete(asyncio.gather(producer_coro, consumer_coro))
loop.run_until_complete(main())
loop.close()
Loading