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

refactor: Asynchronous cog/extension loading. #1132

Open
wants to merge 34 commits into
base: master
Choose a base branch
from

Conversation

elenakrittik
Copy link
Contributor

@elenakrittik elenakrittik commented Nov 12, 2023

Summary

Fixes #641

Refactors the codebase by removing loop sharing where possible and using asyncio.get_running_loop() instead. The loop is still shared in places where the code runs on a separate thread like KeepAliveHandler and AudioPlayer. This means that the majority of the library can now assume that there is an event loop already running. Other changes are documented in changelog files.

While the amount of breaking changes may seem horrific, practically most (if not all) use cases are trivial to fix: users will only need to paint a plenty of async/awaits here and there and possibly move any initialization logic from on_ready, bot constructors, etc. into setup_hook. For an example of migrating see test_bot changes, or for a real-world example see these three commits on my Avrae fork.

Thanks to the addition of Client.loop property bots that currently do something like bot.loop.create_task will not be affected by the changes (as long as that access happens after the bot is started, of course).

While i have tried my best to test this in as many cases as possible, i would appreciate if someone could port their bot and see if there is anything suspicious happening. Notably, i haven't yet tested voice sending features, even though they probably should be fine as-is. Tested it using examples/basic_voice.py, works like a charm now!

I would also appreciate some hints on how/where to document this change, as i believe while it is easy to fix, it is still something that we need to make as many users as possible aware of even outside of the changelog.

As for the "Port #xxxx" commits: this initially started as ports of the according PRs to discord.py, but since the 4th or so commit work was done without "stealing" code from there. Not sure if there's anything legal needed to be done?

Checklist

  • If code changes were made, then they have been tested (mostly)
    • I have updated the documentation to reflect the changes
    • I have formatted the code properly by running pdm lint
    • I have type-checked the code by running pdm pyright
  • This PR fixes an issue
  • This PR adds something new (e.g. new method or parameters)
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@shiftinv shiftinv added t: enhancement New feature t: refactor/typing/lint Refactors, typing changes and/or linting changes labels Nov 13, 2023
@shiftinv shiftinv added the 3.0 label Nov 17, 2023
Signed-off-by: lena <77104725+elenakrittik@users.noreply.github.com>
@@ -0,0 +1 @@
|commands| :meth:`.ext.commands.Cog.cog_load` and :meth:`.ext.commands.Cog.cog_unload` can now be either asynchronous or not.
Copy link
Contributor Author

@elenakrittik elenakrittik May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: is this wording okay?

disnake/client.py Outdated Show resolved Hide resolved
@loop.setter
def loop(self, _value: Never) -> None:
warnings.warn(
"Setting `Client.loop` is deprecated and has no effect. Use `asyncio.get_running_loop()` instead.",
Copy link
Contributor Author

@elenakrittik elenakrittik May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: i'm not sure the suggestion is correct (apart from the typo (get => set), i don't know if set_event_loop will work on-the-fly); might be better to remove the suggestion altogether?

disnake/client.py Outdated Show resolved Hide resolved
disnake/ext/commands/cog.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.0 t: enhancement New feature t: refactor/typing/lint Refactors, typing changes and/or linting changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Async Extension and Cog Loading
2 participants