adding chapter 7 source code

This commit is contained in:
muassif
2021-03-31 09:22:38 +04:00
committed by GitHub
parent 9589675a50
commit f88527eef4
20 changed files with 618 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
#asyncio1.py to build a basic coroutine
import asyncio
import time
async def say(delay, msg):
await asyncio.sleep(delay)
print(msg)
print("Started at ", time.strftime("%X"))
asyncio.run(say(1,"Good"))
asyncio.run(say(2, "Morning"))
print("Stopped at ", time.strftime("%X"))
+17
View File
@@ -0,0 +1,17 @@
#asyncio2.py to build and run coroutines in parallel
import asyncio
import time
async def say(delay, msg):
await asyncio.sleep(delay)
print(msg)
async def main ():
task1 = asyncio.create_task( say(1, 'Good'))
task2 = asyncio.create_task( say(1, 'Morning'))
print("Started at ", time.strftime("%X"))
await task1
await task2
print("Stopped at ", time.strftime("%X"))
asyncio.run(main())
+39
View File
@@ -0,0 +1,39 @@
#asyncio3.py to distribute work via queue
import asyncio
import random
import time
async def executer(name, queue):
while True:
exec_time = await queue.get()
await asyncio.sleep(exec_time)
queue.task_done()
print(f'{name} has taken {exec_time:.2f} seconds')
async def main ():
myqueue = asyncio.Queue()
calc_exuection_time = 0
for _ in range(10):
sleep_for = random.uniform(0.4, 0.8)
calc_exuection_time += sleep_for
myqueue.put_nowait(sleep_for)
tasks = []
for id in range(3):
task = asyncio.create_task(executer(f'Task-{id+1}', myqueue))
tasks.append(task)
start_time = time.monotonic()
await myqueue.join()
total_exec_time = time.monotonic() - start_time
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
print(f"Calculated execution time {calc_exuection_time:0.2f}")
print(f"Actual execution time {total_exec_time:0.2f}")
asyncio.run(main())
+62
View File
@@ -0,0 +1,62 @@
#asyncio_casestudy.py
import asyncio
import time
import aiofiles, aiohttp
from getfilelistpy import getfilelist
TASK_POOL_SIZE = 10
#update the resource object as per your API key and the gdriver folder id
resource = {
"api_key": "AIzaSyDYKmm85keqnk41DpYa2bxddKrGns4z0",
"id": "0B8TxHW2Ci6dbckVweTRlV3RUU",
"fields": "files(name,id,webContentLink)",
}
async def mydownloader(name, queue):
while True:
# Get the file id and name from the queue
item = await queue.get()
try:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
async with session.get(item['webContentLink']) as resp:
if resp.status == 200:
f = await aiofiles.open('./files/{}'.format(
item['name']), mode='wb')
await f.write(await resp.read())
await f.close()
finally:
print(f"{name}: Download completed for ",item['name'])
queue.task_done()
def get_files(resource):
res = getfilelist.GetFileList(resource)
files_list = res['fileList'][0]
return files_list
async def main ():
files = get_files(resource)
#add files info into the queue
myqueue = asyncio.Queue()
for item in files['files']:
myqueue.put_nowait(item)
tasks = []
for id in range(TASK_POOL_SIZE):
task = asyncio.create_task(
mydownloader(f'Task-{id+1}', myqueue))
tasks.append(task)
start_time = time.monotonic()
await myqueue.join()
total_exec_time = time.monotonic() - start_time
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
print(f'Time taken to download: {total_exec_time:.2f} seconds')
asyncio.run(main())