Created
November 7, 2019 12:59
-
-
Save kovalbogdan95/df4888c655df16c53d9b1ee5a7698080 to your computer and use it in GitHub Desktop.
Asyncio example
This file contains hidden or 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
import asyncio | |
import datetime | |
import random | |
async def my_sleep_func(): | |
await asyncio.sleep(random.randint(0, 5)) | |
async def display_date(num, loop): | |
end_time = loop.time() + 50.0 | |
while True: | |
print("Loop: {} Time: {}".format(num, datetime.datetime.now())) | |
if (loop.time() + 1.0) >= end_time: | |
break | |
await my_sleep_func() | |
loop = asyncio.get_event_loop() | |
asyncio.ensure_future(display_date(1, loop)) | |
asyncio.ensure_future(display_date(2, loop)) | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment