for t in asyncio.Task.all_tasks(loop):t.cancel()loop.run_until_complete(asyncio.sleep(0.1)) # Give them a little time to recoverloop.close()
Hi Guido, Hi List,for t in asyncio.Task.all_tasks(loop):t.cancel()loop.run_until_complete(asyncio.sleep(0.1)) # Give them a little time to recoverloop.close()
That solves my problem. Couln't we write def cancel(self): on top of it and put it into
BaseEventLoop? In this case we should even be able to replace
the run_until_complete by a mere self._run_once(), then we don't need to wait
an extra 0.1.
Sure, if tasks start to resist being cancelled, that's a big mess. But I do think there
is quite a number of programmers out there who withstood the temptation to let
tasks resist their cancellation, but who would like to have their finalizers called in
an orderly fashion.
And yet having a try/finally around a yield-from is an easy recipe for resisting cancellation -- it is all too convenient to put another yield-from in the finally clause, and then you are requiring multiple trips through the event loop.
Where are the finalizers you need called and what do they do? And why do you need them called when you're shutting down the process?
Hi Guido, Hi List,And yet having a try/finally around a yield-from is an easy recipe for resisting cancellation -- it is all too convenient to put another yield-from in the finally clause, and then you are requiring multiple trips through the event loop.This is why I would love to have a well-documented function in the standardlibrary, where someone thought of all those corner-cases, and the functioneither handles those, raises an exception or there is something in thedocumentation telling the programmer DON'T DO THAT!
Where are the finalizers you need called and what do they do? And why do you need them called when you're shutting down the process?I am writing an RPC-like system, along the lines of@coroutinedef server(self):open_database()try:yield from getattr(self, yield from read_command())()finally:close_database()now, there is a command to shut down the server, and I would likethe database to be properly closed for every server. First time Idiscovered all of this was actually when I was pressing Ctrl-Cand realized it didn't close the database, something I alwayskept for granted in a finalizer...
I am sure all this can be achieved differently, but nevertheless I thinka well thought out cancel method would still be a very nice thing tohave.