Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #91: Mouse chewed through power cable


comp / comp.lang.python / Re: asyncio and tkinter

SubjectAuthor
o Re: asyncio and tkinterStefan Ram

1
Subject: Re: asyncio and tkinter
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Thu, 4 Jul 2024 18:30 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: asyncio and tkinter
Date: 4 Jul 2024 18:30:47 GMT
Organization: Stefan Ram
Lines: 52
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <asyncio-20240704190556@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de +4aiFzyzsrW1U9VZhKrTjgbA0IYIkhT3XklyejqWbEKTsY
Cancel-Lock: sha1:y0a8vYHrs4w2d2Wn9MHOd8wnRPY= sha256:RzIpZoUlpXiWzU17wdQ3tomONCzZGIp5391qUGP5Ano=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
> def terminate_tasks( self ):
> loop = asyncio.get_running_loop()
> pending = asyncio.all_tasks( loop=loop )
> label_tasks = []
> for task in pending:
> if 'app_example_task' in str( task ):
> label_tasks.append( task )
> task.cancel()
> group = asyncio.gather( *label_tasks, return_exceptions=True )
> try:
> loop.run_until_complete( group )
> except AssertionError:
> # ignoring an assertion error on Windows I do not understand
> pass
in 2023

Yeah, that assertion error! How do you shut down tasks? It seems
everywhere people tell you to call "gather" and "run_until_complete".
But I found that in this case I need to /cancel/ my task and do
nothing else (maybe because I usually want to continue running):

def terminate_tasks( self ):
# Here we're on the event loop, but not in an async method.
loop = asyncio.get_running_loop()
pending = asyncio.all_tasks( loop=loop )
label_tasks = []
# We just send cancels to the tasks we want to cancel.
# We do not want to run all tasks to an end and terminate the
# loop, so there's no need to call anything with "gather" or
# "run" here!
for task in pending:
if 'app_example_task' in str( task ):
loop.call_soon_threadsafe( task.cancel )

. I have still not understood ascyncio. So I found the above out
the hard way: trying everything until it worked!

But I got this idea: There are three cases:

A: you are in a non-async function and not on the event loop.

B: you are in an async function and on the event loop.

C: you are in a non-async function and on the event loop.

The above is case C. And to know how to do things, you sometimes
need to know which of those three cases applies!

(Later, during the execution after "terminate_tasks", my
"app_async_mainloop" still does some waiting, so this might do
something which would otherwise be done by "run_until_complete".)

1

rocksolid light 0.9.8
clearnet tor