Replies: 1 comment
-
If you want the background handler to be called repeatedly (e.g., a background update function), then it needs to start a loop. However - as soon as you do that, your handler becomes blocking, so you can't use a normal function here. That's where async handlers come in. If you make the function async, you can make a background handler that doesn't exit - but does yield control to the event loop periodically; e.g.:
will continue to run in the background, waking up every second to print another line of output. You don't have to make the function fully async, though; If a function yields an integer, Toga will interpret that as a request to sleep; so, that async handler is functionally identical to:
The benefit of the async version over the non-async version is that you can also use any other asycnio function that could potentially block (e.g., an asyncio network request). |
Beta Was this translation helpful? Give feedback.
-
There is official example code in Toga under /examples/handlers.
My questions:
add_background_task(handler)
for exactly?while True:
Docs say:
With the following code, I would expect the function
bkgnd_task_handler
to be called repeatedly, every time thru the event loop, but it only runs once.Beta Was this translation helpful? Give feedback.
All reactions