Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Q on asyncio
#1
Hi,

On line 12 below, I get a warning msg from pycharm stating "Coroutine 'create_task' is not awaited".
If I do modify it with
await asyncio.create_task(getTemperatures(5))
then, the code will not proceed to the next line.

How should I improve the code?

TIA

import asyncio
from datetime import datetime

async def getTemperatures(t):
    while True:
        now = datetime.now()
        print(f'Displaying every 5 secs. {now:%S}')
        await asyncio.sleep(t)


async def main():
    asyncio.create_task(getTemperatures(5))
    # await asyncio.create_task(getTemperatures(5))
    while True:
        print('Clocking every second')
        await asyncio.sleep(1)


def run():
    try:
        asyncio.run(main())
    except KeyboardInterrupt as err:
        print(f'kb interrupted {err}')


if __name__ == "__main__":
    run()
Reply
#2
Not sure what the error is your getting but, this works for me. I'm not using pycharm. I prefer vscodium.

import asyncio 

from datetime import datetime

async def get_temp(num):
    while True:
        now = datetime.now()
        print(f'Display every 5 secs. {now:"%S"}')
        await asyncio.sleep(num)


def run():
    try:
        asyncio.run(get_temp(5))
    except KeyboardInterrupt:
        print(f'Exiting program')


if __name__ == '__main__':
    run()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Yesterday, 11:38 AM)menator01 Wrote: Not sure what the error is your getting but, this works for me. I'm not using pycharm. I prefer vscodium.

import asyncio 

from datetime import datetime

async def get_temp(num):
    while True:
        now = datetime.now()
        print(f'Display every 5 secs. {now:"%S"}')
        await asyncio.sleep(num)


def run():
    try:
        asyncio.run(get_temp(5))
    except KeyboardInterrupt:
        print(f'Exiting program')


if __name__ == '__main__':
    run()


Thanks! It's not actually an error. It just highlights that line as a warning.
So, there's no need to "create a task" as shown in my test code?
Reply
#4
(Yesterday, 10:02 AM)ebolisa Wrote: I get a warning msg from pycharm stating "Coroutine 'create_task' is not awaited".
Never run Asynchronous/Concurrency in editors as they can block.

(Yesterday, 12:20 PM)ebolisa Wrote: Thanks! It's not actually an error. It just highlights that line as a warning.
So, there's no need to "create a task" as shown in my test code?
Code from menator01 will not run in the background,it will run the get_temp coroutine in the foreground,and nothing else will happen concurrently.
For coroutine to run in the background while performing other tasks,as you use need asyncio.create_task().
To eg add like stop/break after 3-minutes.
import asyncio
from datetime import datetime, timedelta

async def temperatures(t, stop_event):
    while not stop_event.is_set():
        now = datetime.now()
        print(f'Displaying every 5 secs. {now:%S}')
        await asyncio.sleep(t)
    print("Stopping temperatures after 3 minutes")

async def main():
    stop_event = asyncio.Event()
    temperature_task = asyncio.create_task(temperatures(5, stop_event))
    start_time = datetime.now()
    while not stop_event.is_set():
        print('Clocking every second')
        await asyncio.sleep(1)
        if datetime.now() >= start_time + timedelta(minutes=3):
            stop_event.set()
    await temperature_task

def run():
    try:
        asyncio.run(main())
    except KeyboardInterrupt as err:
        print(f'kb interrupted {err}')

if __name__ == "__main__":
    run()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020