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

Allow App.startup() to be async #2857

Open
rmartin16 opened this issue Sep 17, 2024 · 1 comment
Open

Allow App.startup() to be async #2857

rmartin16 opened this issue Sep 17, 2024 · 1 comment
Labels
enhancement New features, or improvements to existing features.

Comments

@rmartin16
Copy link
Member

rmartin16 commented Sep 17, 2024

What is the problem or limitation you are having?

An app's startup() method cannot be defined as async. Therefore, to take async actions at startup, users must factor out the async logic to run from the event loop.

Describe the solution you'd like

With the resolution of #2834, Toga now ensures that the event loop is running on all platforms when startup() is invoked. Given this, the entire startup process can now support being async. Specifically, users would be able to define their startup() method as async.

I imagine to accomplish this, the current App._startup() method would need to be broken up. The invocation of App.startup() would be via asyncio.create_task(handler(self.startup)); then the remaining logic in App._startup() would be refactored in to a new method that is added as a callback for the Task for App.startup().

Describe alternatives you've considered

Users can factor out the async logic in to either on_running() or their own custom task.

If this idea is rejected, then Toga should error if startup() is async; currently, it errors because main_window isn't set which is likely to be confusing to users.

Additional context

No response

@rmartin16 rmartin16 added the enhancement New features, or improvements to existing features. label Sep 17, 2024
@freakboy3742
Copy link
Member

Conceptually, I agree it would be desirable to allow startup() to be async. However, I'm not sure it's actually possible to do so.

The problem will be that in order for it to be async, the app loop needs to be running; but on some platforms, in order to be running, it needs to be fully configured with a window. More experimentation is required :-)

I think the implementation could actually be slightly simpler than you suggest: if the internals of _startup() is an async method that then awaits startup() and on_running() if they're co-routines, invoking them directly if they're not, then _startup() itself just needs to create the task for that internal _startup() wrapper.

Regardless, I agree that if startup can't be async, we should raise an error rather than invoke and not await a co-routine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New features, or improvements to existing features.
Projects
None yet
Development

No branches or pull requests

2 participants