- Notifications
You must be signed in to change notification settings - Fork5.7k
Customized concurrent handling#3654
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
33 commits Select commitHold shift + click to select a range
46f5355
add customized concurrent handling
e3245c1
Merge branch 'master' of github.com:python-telegram-bot/python-telegr…
1ccfeae
Merge branch 'master' of github.com:python-telegram-bot/python-telegr…
a8bf427
add some requested changes
ecf2721
add docstrings
f9d8f30
some more improvements
ab95f79
exclude making new tasks when concurrent_updates is 0
2be64f2
requested changes
249caa4
Merge branch 'master' of github.com:python-telegram-bot/python-telegr…
88d11ba
add tests and requested changes
99e556e
fix tests and docstrings
19d47fe
add slots
c922b9c
extend test coverage
5f4ab1d
fix tests
f450e20
Merge branch 'master' into customized-concurrent-handling
clot27a2bf83d
Merge branch 'master' into customized-concurrent-handling
clot273c8f4e5
add tests and some requested changes
9c0fa9a
fix tests
5b7c4a0
really fix them this time
f3a6585
Pimp my tests :)
Bibo-Joshi7a30965
Merge branch 'master' into customized-concurrent-handling
Bibo-Joshid535c46
Fix failing tests
Bibo-Joshibb7caf8
fix docs
b60c119
add test for SUP with 'is'
39b1adb
add final requested changes
6cc2102
Fine tune tests
Bibo-Joshi764a5d0
Add some versioning directives
Bibo-Joshi59db8d3
Merge branch 'master' into customized-concurrent-handling
Bibo-Joshi9aeeb08
typo
Bibo-Joshi168eac4
address final review
550431c
fix pre-commit
d03f81e
Update docs/source/telegram.ext.simpleupdateprocessor.rst
clot27429c023
Merge branch 'master' into customized-concurrent-handling
clot27File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletionsdocs/source/telegram.ext.baseupdateprocessor.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
BaseUpdateProcessor | ||
=================== | ||
.. autoclass:: telegram.ext.BaseUpdateProcessor | ||
:members: | ||
:show-inheritance: |
2 changes: 2 additions & 0 deletionsdocs/source/telegram.ext.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletionsdocs/source/telegram.ext.simpleupdateprocessor.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
SimpleUpdateProcessor | ||
===================== | ||
.. autoclass:: telegram.ext.SimpleUpdateProcessor | ||
:members: | ||
:show-inheritance: |
3 changes: 3 additions & 0 deletionstelegram/ext/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
52 changes: 35 additions & 17 deletionstelegram/ext/_application.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -57,6 +57,7 @@ | ||
from telegram._utils.warnings import warn | ||
from telegram.error import TelegramError | ||
from telegram.ext._basepersistence import BasePersistence | ||
from telegram.ext._baseupdateprocessor import BaseUpdateProcessor | ||
from telegram.ext._contexttypes import ContextTypes | ||
from telegram.ext._extbot import ExtBot | ||
from telegram.ext._handler import BaseHandler | ||
@@ -228,12 +229,11 @@ class Application(Generic[BT, CCT, UD, CD, BD, JQ], AsyncContextManager["Applica | ||
"_chat_data", | ||
"_chat_ids_to_be_deleted_in_persistence", | ||
"_chat_ids_to_be_updated_in_persistence", | ||
"_conversation_handler_conversations", | ||
"_initialized", | ||
"_job_queue", | ||
"_running", | ||
"_update_processor", | ||
"_user_data", | ||
"_user_ids_to_be_deleted_in_persistence", | ||
"_user_ids_to_be_updated_in_persistence", | ||
@@ -259,7 +259,7 @@ def __init__( | ||
update_queue: "asyncio.Queue[object]", | ||
updater: Optional[Updater], | ||
job_queue: JQ, | ||
update_processor: "BaseUpdateProcessor", | ||
persistence: Optional[BasePersistence[UD, CD, BD]], | ||
context_types: ContextTypes[CCT, UD, CD, BD], | ||
post_init: Optional[ | ||
@@ -297,14 +297,7 @@ def __init__( | ||
self.post_stop: Optional[ | ||
Callable[["Application[BT, CCT, UD, CD, BD, JQ]"], Coroutine[Any, Any, None]] | ||
] = post_stop | ||
self._update_processor = update_processor | ||
clot27 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
self.bot_data: BD = self.context_types.bot_data() | ||
self._user_data: DefaultDict[int, UD] = defaultdict(self.context_types.user_data) | ||
self._chat_data: DefaultDict[int, CD] = defaultdict(self.context_types.chat_data) | ||
@@ -359,9 +352,13 @@ def concurrent_updates(self) -> int: | ||
""":obj:`int`: The number of concurrent updates that will be processed in parallel. A | ||
value of ``0`` indicates updates are *not* being processed concurrently. | ||
.. versionchanged:: NEXT.VERSION | ||
This is now just a shortcut to :attr:`update_processor.max_concurrent_updates | ||
<telegram.ext.BaseUpdateProcessor.max_concurrent_updates>`. | ||
.. seealso:: :wiki:`Concurrency` | ||
""" | ||
return self._update_processor.max_concurrent_updates | ||
@property | ||
def job_queue(self) -> Optional["JobQueue[CCT]"]: | ||
@@ -379,12 +376,25 @@ def job_queue(self) -> Optional["JobQueue[CCT]"]: | ||
) | ||
return self._job_queue | ||
@property | ||
def update_processor(self) -> "BaseUpdateProcessor": | ||
""":class:`telegram.ext.BaseUpdateProcessor`: The update processor used by this | ||
application. | ||
.. seealso:: :wiki:`Concurrency` | ||
.. versionadded:: NEXT.VERSION | ||
""" | ||
return self._update_processor | ||
async def initialize(self) -> None: | ||
"""Initializes the Application by initializing: | ||
* The :attr:`bot`, by calling :meth:`telegram.Bot.initialize`. | ||
* The :attr:`updater`, by calling :meth:`telegram.ext.Updater.initialize`. | ||
* The :attr:`persistence`, by loading persistent conversations and data. | ||
* The :attr:`update_processor` by calling | ||
:meth:`telegram.ext.BaseUpdateProcessor.initialize`. | ||
Does *not* call :attr:`post_init` - that is only done by :meth:`run_polling` and | ||
:meth:`run_webhook`. | ||
@@ -397,6 +407,8 @@ async def initialize(self) -> None: | ||
return | ||
await self.bot.initialize() | ||
await self._update_processor.initialize() | ||
if self.updater: | ||
await self.updater.initialize() | ||
@@ -429,6 +441,7 @@ async def shutdown(self) -> None: | ||
* :attr:`updater` by calling :meth:`telegram.ext.Updater.shutdown` | ||
* :attr:`persistence` by calling :meth:`update_persistence` and | ||
:meth:`BasePersistence.flush` | ||
* :attr:`update_processor` by calling :meth:`telegram.ext.BaseUpdateProcessor.shutdown` | ||
Does *not* call :attr:`post_shutdown` - that is only done by :meth:`run_polling` and | ||
:meth:`run_webhook`. | ||
@@ -447,6 +460,8 @@ async def shutdown(self) -> None: | ||
return | ||
await self.bot.shutdown() | ||
await self._update_processor.shutdown() | ||
if self.updater: | ||
await self.updater.shutdown() | ||
@@ -1060,11 +1075,15 @@ async def _update_fetcher(self) -> None: | ||
_LOGGER.debug("Processing update %s", update) | ||
if self._update_processor.max_concurrent_updates > 1: | ||
# We don't await the below because it has to be run concurrently | ||
self.create_task( | ||
self.__process_update_wrapper(update), | ||
update=update, | ||
) | ||
else: | ||
await self.__process_update_wrapper(update) | ||
except asyncio.CancelledError: | ||
# This may happen if the application is manually run via application.start() and | ||
# then a KeyboardInterrupt is sent. We must prevent this loop to die since | ||
@@ -1075,9 +1094,8 @@ async def _update_fetcher(self) -> None: | ||
) | ||
async def __process_update_wrapper(self, update: object) -> None: | ||
await self._update_processor.process_update(update, self.process_update(update)) | ||
self.update_queue.task_done() | ||
async def process_update(self, update: object) -> None: | ||
"""Processes a single update and marks the update to be updated by the persistence later. | ||
41 changes: 33 additions & 8 deletionstelegram/ext/_applicationbuilder.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.