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()
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()
(Jun-30-2024, 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?
(Jun-30-2024, 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.
(Jun-30-2024, 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()
Thank you! Will read up on
stop_event
as I'm not familiar with it.