mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
fix(bot,dispatcher): do not use _MainThread event loop (#397)
* fix(bot,dispatcher): do not use _MainThread event loop on ::Bot, ::Dispatcher initializations * fix: use more generic get approach * docs: comments * chore(task): asyncio.create_task comes with py3.7 * fix(dispatcher): todo
This commit is contained in:
parent
7d1c8c42d3
commit
c99b165668
3 changed files with 53 additions and 20 deletions
|
|
@ -56,6 +56,8 @@ class BaseBot:
|
|||
:type timeout: :obj:`typing.Optional[typing.Union[base.Integer, base.Float, aiohttp.ClientTimeout]]`
|
||||
:raise: when token is invalid throw an :obj:`aiogram.utils.exceptions.ValidationError`
|
||||
"""
|
||||
self._main_loop = loop
|
||||
|
||||
# Authentication
|
||||
if validate_token:
|
||||
api.check_token(token)
|
||||
|
|
@ -66,19 +68,12 @@ class BaseBot:
|
|||
self.proxy = proxy
|
||||
self.proxy_auth = proxy_auth
|
||||
|
||||
# Asyncio loop instance
|
||||
if loop is None:
|
||||
loop = asyncio.get_event_loop()
|
||||
self.loop = loop
|
||||
|
||||
# aiohttp main session
|
||||
ssl_context = ssl.create_default_context(cafile=certifi.where())
|
||||
|
||||
self._session: Optional[aiohttp.ClientSession] = None
|
||||
self._connector_class: Type[aiohttp.TCPConnector] = aiohttp.TCPConnector
|
||||
self._connector_init = dict(
|
||||
limit=connections_limit, ssl=ssl_context, loop=self.loop
|
||||
)
|
||||
self._connector_init = dict(limit=connections_limit, ssl=ssl_context)
|
||||
|
||||
if isinstance(proxy, str) and (proxy.startswith('socks5://') or proxy.startswith('socks4://')):
|
||||
from aiohttp_socks import SocksConnector
|
||||
|
|
@ -106,11 +101,15 @@ class BaseBot:
|
|||
|
||||
def get_new_session(self) -> aiohttp.ClientSession:
|
||||
return aiohttp.ClientSession(
|
||||
connector=self._connector_class(**self._connector_init),
|
||||
loop=self.loop,
|
||||
connector=self._connector_class(**self._connector_init, loop=self._main_loop),
|
||||
loop=self._main_loop,
|
||||
json_serialize=json.dumps
|
||||
)
|
||||
|
||||
@property
|
||||
def loop(self) -> Optional[asyncio.AbstractEventLoop]:
|
||||
return self._main_loop
|
||||
|
||||
@property
|
||||
def session(self) -> Optional[aiohttp.ClientSession]:
|
||||
if self._session is None or self._session.closed:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue