mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 14:35:00 +01:00
Change Default Values for concurrent_updates
and connection_pool_size
(#3127)
This commit is contained in:
parent
df07148e2d
commit
f1d03393de
7 changed files with 23 additions and 7 deletions
10
docs/source/inclusions/pool_size_tip.rst
Normal file
10
docs/source/inclusions/pool_size_tip.rst
Normal file
|
@ -0,0 +1,10 @@
|
|||
.. tip::
|
||||
When making requests to the Bot API in an asynchronous fashion (e.g. via
|
||||
:attr:`block=False <telegram.ext.BaseHandler.block>`, :meth:`Application.create_task <telegram.ext.Application.create_task>`,
|
||||
:meth:`~telegram.ext.ApplicationBuilder.concurrent_updates` or the :class:`~telegram.ext.JobQueue`), it can happen that more requests
|
||||
are being made in parallel than there are connections in the pool.
|
||||
If the number of requests is much higher than the number of connections, even setting
|
||||
:meth:`~telegram.ext.ApplicationBuilder.pool_timeout` to a larger value may not always be enough to prevent pool
|
||||
timeouts.
|
||||
You should therefore set :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates`, :meth:`~telegram.ext.ApplicationBuilder.connection_pool_size` and
|
||||
:meth:`~telegram.ext.ApplicationBuilder.pool_timeout` to values that make sense for your setup.
|
|
@ -172,7 +172,7 @@ class Bot(TelegramObject, AbstractAsyncContextManager):
|
|||
private_key (:obj:`bytes`, optional): Private key for decryption of telegram passport data.
|
||||
private_key_password (:obj:`bytes`, optional): Password for above private key.
|
||||
|
||||
.. include:: bot_methods.rst
|
||||
.. include:: inclusions/bot_methods.rst
|
||||
|
||||
"""
|
||||
|
||||
|
|
|
@ -252,7 +252,7 @@ class Application(Generic[BT, CCT, UD, CD, BD, JQ], AbstractAsyncContextManager)
|
|||
if isinstance(concurrent_updates, int) and concurrent_updates < 0:
|
||||
raise ValueError("`concurrent_updates` must be a non-negative integer!")
|
||||
if concurrent_updates is True:
|
||||
concurrent_updates = 4096
|
||||
concurrent_updates = 256
|
||||
self._concurrent_updates_sem = asyncio.BoundedSemaphore(concurrent_updates or 1)
|
||||
self._concurrent_updates: int = concurrent_updates or 0
|
||||
|
||||
|
|
|
@ -191,7 +191,7 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]):
|
|||
)
|
||||
else:
|
||||
connection_pool_size = (
|
||||
DefaultValue.get_value(getattr(self, f"{prefix}connection_pool_size")) or 128
|
||||
DefaultValue.get_value(getattr(self, f"{prefix}connection_pool_size")) or 256
|
||||
)
|
||||
|
||||
timeouts = dict(
|
||||
|
@ -424,7 +424,9 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]):
|
|||
def connection_pool_size(self: BuilderType, connection_pool_size: int) -> BuilderType:
|
||||
"""Sets the size of the connection pool for the
|
||||
:paramref:`~telegram.request.HTTPXRequest.connection_pool_size` parameter of
|
||||
:attr:`telegram.Bot.request`. Defaults to ``128``.
|
||||
:attr:`telegram.Bot.request`. Defaults to ``256``.
|
||||
|
||||
.. include:: inclusions/pool_size_tip.rst
|
||||
|
||||
Args:
|
||||
connection_pool_size (:obj:`int`): The size of the connection pool.
|
||||
|
@ -504,6 +506,8 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]):
|
|||
:paramref:`~telegram.request.HTTPXRequest.pool_timeout` parameter of
|
||||
:attr:`telegram.Bot.request`. Defaults to :obj:`None`.
|
||||
|
||||
.. include:: inclusions/pool_size_tip.rst
|
||||
|
||||
Args:
|
||||
pool_timeout (:obj:`float`): See
|
||||
:paramref:`telegram.request.HTTPXRequest.pool_timeout` for more information.
|
||||
|
@ -770,11 +774,13 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]):
|
|||
that your bot does not (explicitly or implicitly) rely on updates being processed
|
||||
sequentially.
|
||||
|
||||
.. include:: inclusions/pool_size_tip.rst
|
||||
|
||||
.. seealso:: :attr:`telegram.ext.Application.concurrent_updates`
|
||||
|
||||
Args:
|
||||
concurrent_updates (:obj:`bool` | :obj:`int`): Passing :obj:`True` will allow for
|
||||
``4096`` updates to be processed concurrently. Pass an integer to specify a
|
||||
``256`` updates to be processed concurrently. Pass an integer to specify a
|
||||
different number of updates that may be processed concurrently.
|
||||
|
||||
Returns:
|
||||
|
|
|
@ -139,7 +139,7 @@ class TestApplication:
|
|||
assert recwarn[0].filename == __file__, "stacklevel is incorrect!"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"concurrent_updates, expected", [(0, 0), (4, 4), (False, 0), (True, 4096)]
|
||||
"concurrent_updates, expected", [(0, 0), (4, 4), (False, 0), (True, 256)]
|
||||
)
|
||||
@pytest.mark.filterwarnings("ignore: `Application` instances should")
|
||||
def test_init(self, bot, concurrent_updates, expected):
|
||||
|
|
|
@ -93,7 +93,7 @@ class TestApplicationBuilder:
|
|||
)
|
||||
|
||||
client = app.bot.request._client
|
||||
assert client.limits == httpx.Limits(max_connections=128, max_keepalive_connections=128)
|
||||
assert client.limits == httpx.Limits(max_connections=256, max_keepalive_connections=256)
|
||||
assert client.proxies is None
|
||||
assert client.timeout == httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=1.0)
|
||||
|
||||
|
|
Loading…
Reference in a new issue