call_on_close should work with async
Trying to fire an event when a command is finished. I currently use
@cli.resultcallback()
async def console_shutdown(result, **kwargs):
print('cli end')
Which works fine except for when there is no actual command. In other words just running click without command or params to show help output for example.
I need to run code at the end of command including no command which shows help output. I found that click has call_on_close where you can pass in a callback function which does indeed get called at the end of every command even NO command that just shows the help output.
@click.group(...)
@click.pass_context
async def cli(ctx):
ctx.call_on_close(console_shutdown)
But asyncclick does not handle that callback as await. So I can only have synchronous methods. I need the callbacks to be awaited so I can call other shutdown code that must also be awaited.
Basically your core.py __aexit__ needs to call await self.close() and the def close needs to be async def close and each cb in self._close_callbacks needs to be checked if a coroutine, if so await cb(), if not cb().
Thanks for the report, I'll look into handling this.
Had any change to dig into this one? I could make a PR for you but new sure which is your latest branch. Pypi says 8.0.1.3 but there is no such branch/tag in this repo.
Now I see the 8.0.1.3 tag, missed it. Would you like a PR or can you make the 3 lines of code change. Suppose I could also fork if needed, but I really hate doing that to a project for 3 lines of code :)
Looking at the 8.x code its quite different that the 7.x code this issue was opened for. My suggested fix above is definitely not correct for 8.x. But I can verify call_on_close still does not handle async callbacks as I believe it should be able to handle either async or sync.
A PR would be useful, I'm somewhat swamped with $DAY_JOB until the end of next week at least.
My fix is for your 7.x version. You could at least take that and put out a 7.1.2.4 so those that rely on 7.x will have this fix. But the 8.x branch is quite different, I don't have a PR for that. I'll stick on 7.x for the foreseeable future. I notice you don't have a 7.1.2.3 tag on github by pypi shows 7.1.2.3. I assume the PR would be from the 7.1.2.2+async tag?
OK so I planned to look into this but I have no idea any more what the problem is.
If you have a complete piece of example code how to trigger the problem that'd be helpful.