Commit 444c887a2c617819aa0c00da3974ec78e111c5c0

Authored by Lars Tiede
0 parents

first commit

1_just_some_coros.py 0 → 100755
  1 +++ a/1_just_some_coros.py
  1 +#!/usr/bin/env python
  2 +import asyncio
  3 +import random
  4 +
  5 +async def slow_op(n):
  6 + print("slow_op {} started".format(n))
  7 + await asyncio.sleep(random.uniform(0, 2))
  8 + print("slow_op {} complete".format(n))
  9 +
  10 +async def main():
  11 + await asyncio.wait([
  12 + slow_op(i) for i in range(10)
  13 + ])
  14 +
  15 +loop = asyncio.get_event_loop()
  16 +loop.run_until_complete(main())
... ...
2_chaining_with_await.py 0 → 100755
  1 +++ a/2_chaining_with_await.py
  1 +#!/usr/bin/env python
  2 +import asyncio
  3 +import aiohttp
  4 +
  5 +async def get_page(url):
  6 + try:
  7 + print("get {}".format(url))
  8 + response = await aiohttp.get(url)
  9 + print("got {}".format(url))
  10 + return await response.text()
  11 +
  12 + except aiohttp.errors.ClientError as e:
  13 + print("error getting {}: {}".format(url, e))
  14 + return ""
  15 +
  16 +
  17 +async def text_length(url):
  18 + text = await get_page(url)
  19 + return len(text)
  20 +
  21 +
  22 +async def text_lengths(urls):
  23 + lengths = await asyncio.gather(*[
  24 + text_length(url) for url in urls
  25 + ])
  26 + for u, l in zip(urls, lengths):
  27 + print("{}: {} bytes text".format(u, l))
  28 +
  29 +
  30 +urls = [
  31 + "https://www.uit.no",
  32 + "https://www.uio.no",
  33 + "https://www.uib.no",
  34 + "https://www.ntnu.no",
  35 + "http://utviklerlunsj.uit.no",
  36 + "https://www.ihopethisdoesntexist.no/",
  37 + ]
  38 +loop = asyncio.get_event_loop()
  39 +loop.run_until_complete(text_lengths(urls))
... ...
3_no_locks.py 0 → 100755
  1 +++ a/3_no_locks.py
  1 +#!/usr/bin/env python
  2 +import asyncio
  3 +import random
  4 +
  5 +finished_count = 0
  6 +
  7 +async def slow_op(n):
  8 + global finished_count
  9 + print("slow_op {} started".format(n))
  10 + await asyncio.sleep(random.uniform(0.0, 1.0))
  11 + finished_count += 1
  12 + print("slow_op {} complete".format(n))
  13 +
  14 +async def main():
  15 + await asyncio.wait([
  16 + slow_op(i) for i in range(10)
  17 + ])
  18 +
  19 +loop = asyncio.get_event_loop()
  20 +loop.run_until_complete(main())
  21 +print("finished_count = {}".format(finished_count))
... ...
4_100k_coros.py 0 → 100755
  1 +++ a/4_100k_coros.py
  1 +#!/usr/bin/env python
  2 +import asyncio
  3 +
  4 +finished_count = 0
  5 +
  6 +
  7 +
  8 +async def slow_op(n):
  9 + global finished_count
  10 + await asyncio.sleep(1)
  11 + finished_count += 1
  12 +
  13 +
  14 +async def main():
  15 + await asyncio.wait([
  16 + slow_op(i) for i in range(100 * 1000)
  17 + ])
  18 +
  19 +
  20 +
  21 +
  22 +
  23 +
  24 +loop = asyncio.get_event_loop()
  25 +loop.run_until_complete(main())
  26 +print("finished_count = {}".format(finished_count))
... ...
4_100k_threads.py 0 → 100755
  1 +++ a/4_100k_threads.py
  1 +#!/usr/bin/env python
  2 +import threading
  3 +import time
  4 +
  5 +finished_count = 0
  6 +finished_count_lock = threading.Lock()
  7 +
  8 +def slow_op(n):
  9 + global finished_count
  10 + time.sleep(1)
  11 + with finished_count_lock:
  12 + finished_count += 1
  13 +
  14 +def main():
  15 + threads = [
  16 + threading.Thread(target=slow_op, args=(i,))
  17 + for i in range(100 * 1000)
  18 + ]
  19 + for t in threads:
  20 + t.start()
  21 + for t in threads:
  22 + t.join()
  23 +
  24 +main()
  25 +print("finished_count = {}".format(finished_count))
... ...
README.md 0 → 100644
  1 +++ a/README.md
  1 +## Utviklerlunsj: Programming with coroutines and asynchronous I/O
  2 +
  3 +[Slides](https://docs.google.com/presentation/d/1Q2y6fiGCcx87GWd8ipe55chilhiGJHIgSlZ2Ln2MhNU/edit?usp=sharing)
  4 +
  5 +In this repository are the code examples.
  6 +
  7 +
  8 +### Run the examples
  9 +
  10 +You need Python 3.5 for the examples. It's the newest production version of Python that was finalized in September 2015, so you might not have it yet. If you happen to run Ubuntu Linux, I can recommend [this PPA](https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes) to get Python 3.5 on a distro version that doesn't have it yet.
  11 +
  12 +For example 2, you need the pip packages listed in [pip3-req.txt](pip3-req.txt). `pip3 install -r pip3-req.txt` should do the trick. Preferably locally in a virtualenv:
  13 +
  14 +```bash
  15 +python3.5 -m venv py-env
  16 +. ./py-env/bin/activate
  17 +pip3 install -r pip3-req.txt
  18 +```
... ...
pip3-req.txt 0 → 100644
  1 +++ a/pip3-req.txt
  1 +aiohttp
... ...