-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1641d4c
commit 9ff2f46
Showing
11 changed files
with
176 additions
and
47 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|