Skip to content

Commit

Permalink
progress commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantBaheti committed Sep 6, 2021
1 parent 1641d4c commit 9ff2f46
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 47 deletions.
18 changes: 0 additions & 18 deletions AsyncioDemo/async2.py

This file was deleted.

26 changes: 0 additions & 26 deletions AsyncioDemo/async3.py

This file was deleted.

22 changes: 22 additions & 0 deletions AsyncioDemo/async_coroutine1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import asyncio
import random

def print_random():
print(random.randint(1, 10))

async def main():
print('start')

await asyncio.sleep(1)

i = 0
while i< 5:
print_random()

i += 1

await asyncio.sleep(1)
print('end')

if __name__== "__main__":
asyncio.run(main())
16 changes: 16 additions & 0 deletions AsyncioDemo/async_event_loop1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import asyncio
import datetime

INTERVAL = 5

async def get_data(sleep_sec):
await asyncio.sleep(sleep_sec)
print(datetime.datetime.now())

async def main():
while True:
await get_data(INTERVAL)


if __name__ == '__main__':
asyncio.run(main())
5 changes: 2 additions & 3 deletions AsyncioDemo/async1.py → AsyncioDemo/async_future1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@


async def main():
print('hello')
await asyncio.sleep(1)
print('world')

asyncio.run(main())
if __name__ == '__main__':
asyncio.run(main())
11 changes: 11 additions & 0 deletions AsyncioDemo/async_stream1_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import asyncio

async def main():

loop = asyncio.get_event_loop()
print(loop)
await asyncio.sleep(5)


if __name__ == '__main__':
asyncio.run(main())
54 changes: 54 additions & 0 deletions AsyncioDemo/async_stream1_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import asyncio
import datetime
import json
import random

INTERVAL = 2


async def get_data(sleep_sec):
await asyncio.sleep(sleep_sec)
return {
'timestamp' : str(datetime.datetime.now()),
'value' : random.randint(1,100)
}


async def handle_connection(reader,writer):
data = await reader.read(100)
message = data.decode().strip()
addr = writer.get_extra_info('peername')

if message == "please":
print("Starting the connection with", addr)
writer.write(b'Connected successfully.')

while True:
try:
payload = await get_data(INTERVAL)
writer.write(bytes(json.dumps(payload), 'utf-8'))
await writer.drain()

del payload

except Exception as e:
print(e)
break

print("Closing the connection with", addr)
writer.write(b'Connection failed.')
writer.close()


async def main():
server = await asyncio.start_server(handle_connection, '0.0.0.0', 8888)

addr = server.sockets[0].getsockname()
print(f'Serving on {addr}')

async with server:
await server.serve_forever()


if __name__ == '__main__':
asyncio.run(main())
21 changes: 21 additions & 0 deletions AsyncioDemo/async_task1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import asyncio
import datetime as dt


async def wait_for(sec):
await asyncio.sleep(sec)
print("waited for :",sec,"seconds, Now",dt.datetime.now())

async def main():
task_first = asyncio.create_task(wait_for(5))
task_second = asyncio.create_task(wait_for(1))
task_third = asyncio.create_task(wait_for(10))

await task_first
await task_second
await task_third


if __name__ == "__main__":
print("running tasks")
asyncio.run(main())
23 changes: 23 additions & 0 deletions AsyncioDemo/aysnc_task2_ gather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import asyncio
import datetime
import random

async def say_after(sec):
await asyncio.sleep(sec)
print("Waited for :",sec," seconds, ",datetime.datetime.utcnow())


async def main(n_tasks):

task_list = []
for _ in range(n_tasks):
task_list.append(say_after(random.randint(1,50)))

l = await asyncio.gather(*task_list)
print(l)


if __name__ == '__main__':
print("starting at",datetime.datetime.utcnow())
asyncio.run(main(10))
print("ending at", datetime.datetime.utcnow())
10 changes: 10 additions & 0 deletions AsyncioDemo/interval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@



async function getDate(){
let dateValues = new Date();
console.log(dateValues);
}


setInterval(getDate,1000)
17 changes: 17 additions & 0 deletions AsyncioDemo/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# asyncio

## awaitables

There are three main types of awaitable objects
- coroutines
- Tasks
- Futures

## running jobs







0 comments on commit 9ff2f46

Please sign in to comment.