Can use in TaskGroup #81
Answered
by
insolor
musimasami
asked this question in
Q&A
-
Is this code OK? Code from tkinter import *
from tkinter import messagebox
import asyncio
from async_tkinter_loop import async_handler,main_loop
ON_message = 'ON message'
OFF_message = 'OFF message'
async def get_ON(OnOff_que):
while True:
msg = await OnOff_que.get()
print(msg)
async def main():
OnOff_que = asyncio.Queue()
root = Tk()
root.title('My Home Temperature')
root.geometry("400x300")
@async_handler
async def on_info():
res = messagebox.showinfo(title="message", message="ON Aircon")
await OnOff_que.put(ON_message)
print(res)
@async_handler
async def off_info():
res = messagebox.showinfo(title="message", message="OFF Aircon")
await OnOff_que.put(OFF_message)
print(res)
Button(root, text='ON', command=on_info).grid()
Button(root, text='OFF', command=off_info).grid()
async with asyncio.TaskGroup() as tg:
tsk0 = tg.create_task(main_loop(root))
tsk1 = tg.create_task(get_ON(OnOff_que))
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
Answered by
insolor
Jan 24, 2024
Replies: 1 comment 1 reply
-
I think, yes, you can try to use task groups. In examples I used a different approach to run a background task: start_stop_counter.py. But in genereal, I think, yes, using of |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
insolor
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think, yes, you can try to use task groups. In examples I used a different approach to run a background task: start_stop_counter.py. But in genereal, I think, yes, using of
TaskGroup
is totally fine.