asyncclick icon indicating copy to clipboard operation
asyncclick copied to clipboard

call_on_close should work with async

Open mreschke opened this issue 4 years ago • 8 comments

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.

mreschke avatar May 02 '21 04:05 mreschke

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().

mreschke avatar May 02 '21 04:05 mreschke

Thanks for the report, I'll look into handling this.

smurfix avatar May 02 '21 18:05 smurfix

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.

mreschke avatar Jun 30 '21 17:06 mreschke

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 :)

mreschke avatar Jun 30 '21 17:06 mreschke

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.

mreschke avatar Jun 30 '21 17:06 mreschke

A PR would be useful, I'm somewhat swamped with $DAY_JOB until the end of next week at least.

smurfix avatar Jul 01 '21 08:07 smurfix

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?

mreschke avatar Jul 07 '21 19:07 mreschke

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.

smurfix avatar Jan 16 '24 08:01 smurfix