This repository has been archived by the owner on Jun 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 474
Timeout context manager error using sanic and telepot same time #359
Comments
As a temporary fix I made this workaround: import asyncio
import aiohttp
import async_timeout
import telepot.aio
from telepot.aio.api import (
_proxy, _parse, _compose_timeout, _compose_data, _methodurl
)
from telepot.exception import TelegramError
class Bot(telepot.aio.Bot):
async def request(self, req, **user_kw):
fn, args, kwargs, timeout, cleanup = self._transform(req, **user_kw)
if _proxy:
kwargs['proxy'] = _proxy[0]
if len(_proxy) > 1:
kwargs['proxy_auth'] = aiohttp.BasicAuth(*_proxy[1])
try:
if timeout is None:
async with fn(*args, **kwargs) as r:
return await _parse(r)
else:
try:
with async_timeout.timeout(timeout):
async with fn(*args, **kwargs) as r:
return await _parse(r)
except asyncio.TimeoutError:
raise TelegramError('Response timeout', 504, {})
except aiohttp.ClientConnectionError:
raise TelegramError('Connection Error', 400, {})
finally:
if cleanup:
cleanup() # e.g. closing one-time session
async def _api_request(self, method, params=None, files=None, **kwargs):
return await self.request((self._token, method, params, files), **kwargs)
def _transform(self, req, **user_kw):
timeout = _compose_timeout(req, **user_kw)
data = _compose_data(req, **user_kw)
url = _methodurl(req, **user_kw)
session = aiohttp.ClientSession(
connector=aiohttp.TCPConnector(limit=1, force_close=True),
loop=self.loop)
cleanup = session.close
kwargs = {'data': data}
kwargs.update(user_kw)
return session.post, (url,), kwargs, timeout, cleanup Now Bot doesn't lose loop. I'm not sure that it should be fixed in the code of the telepot, but if you want to make it working inside other asyncio frameworks I may have a reason to make loop linked to the calls made by the aiohttp. |
@xen Could you share a diff? $ git diff > issue359 And then drop the file into the comment area or edit the issue. |
File attach didn't work well. So I made the pull request #362. I don't know how to attach it to this issue. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I'm trying to create a web app that communicates with Telegram. And trying to use Sanic web framework with Telepot. Both are asyncio based. Now I'm getting a very weird error.
This is my code:
The error that I'm getting is:
I'm not sure that it is the issue with the telepot. But probably you have an idea how to deal with it.
The text was updated successfully, but these errors were encountered: