From feec19b7f414f0edf3e889ff07e8a44c8a02d0b4 Mon Sep 17 00:00:00 2001 From: the31k Date: Wed, 19 Jul 2017 01:35:44 +0300 Subject: [PATCH] Use per-thread requests sessions Reason is requests.Session is not thread-safe See: https://github.com/requests/requests/issues/2766 --- telebot/apihelper.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index e554730..d1b83a0 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -18,7 +18,6 @@ from telebot import types from telebot import util logger = telebot.logger -req_session = requests.session() proxy = None API_URL = "https://api.telegram.org/bot{0}/{1}" @@ -28,6 +27,10 @@ CONNECT_TIMEOUT = 3.5 READ_TIMEOUT = 9999 +def _get_req_session(): + return util.per_thread('req_session', lambda: requests.session()) + + def _make_request(token, method_name, method='get', params=None, files=None, base_url=API_URL): """ Makes a request to the Telegram API. @@ -47,8 +50,8 @@ def _make_request(token, method_name, method='get', params=None, files=None, bas if params: if 'timeout' in params: read_timeout = params['timeout'] + 10 if 'connect-timeout' in params: connect_timeout = params['connect-timeout'] + 10 - result = req_session.request(method, request_url, params=params, files=files, - timeout=(connect_timeout, read_timeout), proxies=proxy) + result = _get_req_session().req_session.request(method, request_url, params=params, files=files, + timeout=(connect_timeout, read_timeout), proxies=proxy) logger.debug("The server returned: '{0}'".format(result.text.encode('utf8'))) return _check_result(method_name, result)['result'] @@ -97,7 +100,7 @@ def get_file(token, file_id): def download_file(token, file_path): url = FILE_URL.format(token, file_path) - result = req_session.get(url) + result = _get_req_session().get(url) if result.status_code != 200: msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]' \ .format(result.status_code, result.reason, result.text)