├── coroutine ├── kind_of_asyncio.py ├── send_gen.py └── sum_coroutine.py ├── example ├── example.py └── with.py ├── fetcher ├── fetcher_async_01.py ├── fetcher_async_final.py └── fetcher_sync.py ├── requirements.txt └── server ├── server_get_wiki.py └── server_initial.py /coroutine/kind_of_asyncio.py: -------------------------------------------------------------------------------- 1 | def coroutine(function): 2 | def wrapper(*args, **kwargs): 3 | gen = function(*args, **kwargs) 4 | next(gen) # или g.send(None) 5 | return gen 6 | return wrapper 7 | -------------------------------------------------------------------------------- /coroutine/send_gen.py: -------------------------------------------------------------------------------- 1 | 2 | def simple_gen_01(): 3 | value = yield 4 | print('simple generator received:', value) 5 | 6 | def simple_gen_02(): 7 | x = 'simple generator start value' 8 | value = yield x 9 | print('simple generator received:', value) 10 | -------------------------------------------------------------------------------- /coroutine/sum_coroutine.py: -------------------------------------------------------------------------------- 1 | def sum(): 2 | sum_values = 0 3 | 4 | while True: 5 | try: 6 | new_value = yield sum_values 7 | except StopIteration: 8 | break 9 | else: 10 | sum_values += new_value 11 | return sum_values 12 | 13 | -------------------------------------------------------------------------------- /example/example.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import datetime 3 | import random 4 | 5 | @asyncio.coroutine 6 | def display_date(num, loop): 7 | while True: 8 | print("Coroutine: {} Time: {}".format(num, datetime.datetime.now())) 9 | yield from asyncio.sleep(random.randint(0, 5)) 10 | 11 | loop = asyncio.get_event_loop() 12 | 13 | asyncio.ensure_future(display_date(1, loop)) 14 | asyncio.ensure_future(display_date(2, loop)) 15 | asyncio.ensure_future(display_date(3, loop)) 16 | 17 | loop.run_forever() -------------------------------------------------------------------------------- /example/with.py: -------------------------------------------------------------------------------- 1 | from contextlib import contextmanager 2 | 3 | @contextmanager 4 | def file_open(path): 5 | try: 6 | file_object = open(path, 'w') 7 | yield file_object 8 | except OSError: 9 | print("Error!") 10 | finally: 11 | print('Closing file') 12 | file_object.close() 13 | -------------------------------------------------------------------------------- /fetcher/fetcher_async_01.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | import asyncio 3 | 4 | import aiohttp 5 | 6 | from fetcher_sync import imgs, base_url 7 | 8 | async def fetch_content(url, session): 9 | pass 10 | 11 | 12 | async def main(): 13 | pass 14 | 15 | if __name__ == '__main__': 16 | t0 = time() 17 | asyncio.run(main()) 18 | print(time() - t0) 19 | -------------------------------------------------------------------------------- /fetcher/fetcher_async_final.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | import asyncio 3 | 4 | import aiohttp 5 | 6 | from fetcher_sync import imgs, base_url 7 | 8 | async def fetch_content(url, session): 9 | async with session.get(url, allow_redirects=True) as response: 10 | data = await response.read() 11 | 12 | 13 | async def main(): 14 | async with aiohttp.ClientSession() as session: 15 | tasks = [] 16 | for img in imgs: 17 | task = asyncio.create_task(fetch_content(base_url + img, session)) 18 | tasks.append(task) 19 | 20 | await asyncio.gather(*tasks) 21 | 22 | if __name__ == '__main__': 23 | t0 = time() 24 | asyncio.run(main()) 25 | print(time() - t0) 26 | -------------------------------------------------------------------------------- /fetcher/fetcher_sync.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | import requests 3 | 4 | imgs = [ 5 | '101224853437733771076784084333435073849.png', 6 | '234203110511594375148172428602191164045.png', 7 | '76871812872716158449928525126897808669.png', 8 | '4564975574168210989440924446095880845.png', 9 | '237496984764704161987686789975177213053.png', 10 | '320553200938376537527176999859909318800.png', 11 | '293197825000519425313026603016757467280.png', 12 | '222929545278291486237488252477554579600.png', 13 | '100178477904924105351311777736178727290.png', 14 | '44507782717290083674440397381805299856.png', 15 | '185472365654562414785342247165034780816.png', 16 | '210445419674116985778980995126006577530.png', 17 | '307922809846539578245604252414762482042.png', 18 | '149428859046719019945491461367084397693.png', 19 | '261692750296572155100066155182902785309.png', 20 | '248760819828884543776967634368290403453.png', 21 | '147007489053129378869085619512211453053.png', 22 | '336436598020241461413680460569086982522.png', 23 | '291265621724971136572245269034427789965.png', 24 | '101224853437733771076784084333435073849.png', 25 | '234203110511594375148172428602191164045.png', 26 | '76871812872716158449928525126897808669.png', 27 | '4564975574168210989440924446095880845.png', 28 | '237496984764704161987686789975177213053.png', 29 | '320553200938376537527176999859909318800.png', 30 | '293197825000519425313026603016757467280.png', 31 | '222929545278291486237488252477554579600.png', 32 | '100178477904924105351311777736178727290.png', 33 | '44507782717290083674440397381805299856.png', 34 | '185472365654562414785342247165034780816.png', 35 | '210445419674116985778980995126006577530.png', 36 | '307922809846539578245604252414762482042.png', 37 | '149428859046719019945491461367084397693.png', 38 | '261692750296572155100066155182902785309.png', 39 | '248760819828884543776967634368290403453.png', 40 | '147007489053129378869085619512211453053.png', 41 | '336436598020241461413680460569086982522.png', 42 | '291265621724971136572245269034427789965.png', 43 | '101224853437733771076784084333435073849.png', 44 | '234203110511594375148172428602191164045.png', 45 | '76871812872716158449928525126897808669.png', 46 | '4564975574168210989440924446095880845.png', 47 | '237496984764704161987686789975177213053.png', 48 | '320553200938376537527176999859909318800.png', 49 | '293197825000519425313026603016757467280.png', 50 | '222929545278291486237488252477554579600.png', 51 | '100178477904924105351311777736178727290.png', 52 | '44507782717290083674440397381805299856.png', 53 | '185472365654562414785342247165034780816.png', 54 | '210445419674116985778980995126006577530.png', 55 | '307922809846539578245604252414762482042.png', 56 | '149428859046719019945491461367084397693.png', 57 | '261692750296572155100066155182902785309.png', 58 | '248760819828884543776967634368290403453.png', 59 | '147007489053129378869085619512211453053.png', 60 | '336436598020241461413680460569086982522.png', 61 | '291265621724971136572245269034427789965.png', 62 | '139962910803374471327129236685376238877.png' 63 | ] 64 | 65 | base_url = 'https://hhcdn.ru/i-advdream/' 66 | 67 | def get_file(url): 68 | r = requests.get(url, allow_redirects=True) 69 | 70 | def main(): 71 | t0 = time() 72 | for img in imgs: 73 | get_file(base_url + img) 74 | 75 | print(time() - t0) 76 | 77 | 78 | if __name__ == '__main__': 79 | main() 80 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.21.0 2 | aiohttp==3.5.4 -------------------------------------------------------------------------------- /server/server_get_wiki.py: -------------------------------------------------------------------------------- 1 | from aiohttp import web, ClientSession 2 | 3 | async def hello(request): 4 | return web.Response(text="Hello, world") 5 | 6 | async def c10k(request): 7 | async with ClientSession() as session: 8 | async with session.get('https://en.wikipedia.org/wiki/C10k_problem', allow_redirects=True) as response: 9 | html = await response.text() 10 | return web.Response(text=html, headers={'Content-Type': 'text/html'}) 11 | 12 | handlers = [ 13 | web.get('/', hello), 14 | web.get('/c10k', c10k), 15 | ] 16 | 17 | app = web.Application() 18 | app.add_routes(handlers) 19 | 20 | web.run_app(app) 21 | -------------------------------------------------------------------------------- /server/server_initial.py: -------------------------------------------------------------------------------- 1 | from aiohttp import web 2 | 3 | async def hello(request): 4 | return web.Response(text="Hello, world") 5 | 6 | app = web.Application() 7 | app.add_routes([web.get('/', hello)]) 8 | 9 | web.run_app(app) 10 | --------------------------------------------------------------------------------