Make the largest loops lazier and async to avoid blocking #3767
Labels
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot!3767
Loading…
Reference in a new issue
No description provided.
Delete branch "no-block-loops"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type
Description of the changes
This one depends on #3766
@ -55,7 +56,7 @@ class Announcer:async def announcer(self):It would be better to use
asyncio.gatherhere but I guess it's kind of out of scope for this PR.@ -8,3 +8,4 @@import discordfrom redbot.core import commands, i18n, checks, modlogfrom redbot.core.utils import AsyncIterfrom redbot.core.utils.chat_formatting import pagify, humanize_number, boldIt's a nitpick but I think it would be clearer to do
from redbot.core.utils import AsyncGen@ -37,25 +38,25 @@ class GlobalUniqueObjectFinder(commands.Converter):if user is not None:This and the
all_roleslist that was added above can be simplified to:That also means that
all_rolesfrom above can be removed.This whole thing should be done differently - we should stop when we figure out there's more than one match.
I didn't test this but I doubt this works at all -
any()can be used with regular iterables, not with async iterables.Same as above, we should stop iterating when figure out there's more than one match.
@ -18,9 +19,10 @@ from typing import (Union,Set,TYPE_CHECKING,It's not a part of the PR, but could you add
bounded_gather_iterto__all__?Incorrect type hint, it should be:
itemwill be clearer thani, especially considering we already useifor index.itemwill be clearer thani, especially considering we already useifor index.Add type hint (remember to import
Generatorfromtyping)Add type hint (you need to add
from __future__ import annotationsat the top of the file):Considering we're passing iterable (iterator is an iterable), I think it would be clearer to call it
iterableinstead ofcontentsI also have two suggestions for typings here:
a) we can simplify
Union[Iterator[_T], Iterable[_T]]to justIterable[_T]as all iterators are iterableb) since
floatis not anint, we need to specify union of both here if we want to accept both (and since it's only used forasyncio.sleep(), I think we do want it)I think it would be clearer if this attribute were called
iterator(oritif you prefer short version)This needs a better name - this doesn't remove duplicates, it gives you iterator without duplicated entries, maybe it should be called
without_duplicates()?Add type hint:
I think all these attributes should be private.
async_enumeratewould be better here just so that all the logic is in one place.startis missing type hintYou need to return the result of the
coro.__await__()notcoroitself from__await__function:Better docstring needed (list parameters and their expected types as with any other function in this file)
We should also be a little more descriptive about what this does. Something more like (still needs to be better worded):
Last but not least, you need to add
automethoddirective for this class todocs/framework_utils.rstThis class is not just
AsyncIterablebut alsoAsyncIteratorso that generic suits it better. It can also be awaited so I think you should addAwaitablegeneric as well.Since we don't care about order in
_temphere, can you usesetfor better performance?This class name (
AsyncGen) might be confusing - despite of what the name says, this isn't async generator, it's async iterator (and these two terms are different).async_filter()function's type hint is incorrect*args, **kwargsare unnecessary here, if someone wants to, they can pass partial themselves.async_filter()function:@ -37,25 +38,25 @@ class GlobalUniqueObjectFinder(commands.Converter):if user is not None:This is out of the scope of this PR no? I mean im happy to address it but .. just want to be sure
@ -10,6 +10,17 @@ General Utility.. automodule:: redbot.core.utils:members: deduplicate_iterables, bounded_gather, bounded_gather_iter.. autoclass:: AsyncIterFor the methods to show as

async for ... in enumerate(start=)we need to manually indicate it for these two methods:@ -255,3 +257,131 @@ def bounded_gather(tasks = (_sem_wrapper(semaphore, task) for task in coros_or_futures)bad indentation here
Let's use 4 space indent here
Wouldn't it make more sense to call this
self._iterator?@ -258,0 +339,4 @@def enumerate(self, start: int = 0) -> AsyncIterator[Tuple[int, _T]]:"""Async iterable version of `enumerate`.This looks incomplete, maybe you wanted to add a
print()here?@ -258,0 +371,4 @@-------->>> from redbot.core.utils import AsyncIter>>> iterator = AsyncIter([1,2,3,3,4,4,5])>>> async for i in iterator.without_duplicates():This looks incomplete, maybe you wanted to add a
print()here?This looks incomplete, maybe you wanted to add a
print()here?@ -255,3 +257,131 @@ def bounded_gather(tasks = (_sem_wrapper(semaphore, task) for task in coros_or_futures)@ -37,25 +38,25 @@ class GlobalUniqueObjectFinder(commands.Converter):if user is not None:This isn't actually a valid code -
*can't be used with async iterators@ -255,3 +257,131 @@ def bounded_gather(tasks = (_sem_wrapper(semaphore, task) for task in coros_or_futures)