Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call_on_close should work with async #16

Open
mreschke opened this issue May 2, 2021 · 8 comments
Open

call_on_close should work with async #16

mreschke opened this issue May 2, 2021 · 8 comments

Comments

@mreschke
Copy link

mreschke commented May 2, 2021

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
Copy link
Author

mreschke commented May 2, 2021

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

@smurfix
Copy link
Collaborator

smurfix commented May 2, 2021

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

@mreschke
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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.

@smurfix
Copy link
Collaborator

smurfix commented Jul 1, 2021

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

@mreschke
Copy link
Author

mreschke commented Jul 7, 2021

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?

@smurfix
Copy link
Collaborator

smurfix commented Jan 16, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants