diff --git a/telegram/ext/dispatcher.py b/telegram/ext/dispatcher.py index 73e2fb096e1..4392229599a 100644 --- a/telegram/ext/dispatcher.py +++ b/telegram/ext/dispatcher.py @@ -166,7 +166,8 @@ def _init_async_threads(self, base_name, workers): base_name = '{}_'.format(base_name) if base_name else '' for i in range(workers): - thread = Thread(target=self._pooled, name='{}{}'.format(base_name, i)) + thread = Thread(target=self._pooled, name='Bot:{}:worker:{}{}'.format(self.bot.id, + base_name, i)) self.__async_threads.add(thread) thread.start() diff --git a/telegram/ext/jobqueue.py b/telegram/ext/jobqueue.py index e3cfacfd9ae..cbdabae67a4 100644 --- a/telegram/ext/jobqueue.py +++ b/telegram/ext/jobqueue.py @@ -277,7 +277,8 @@ def start(self): if not self._running: self._running = True self.__start_lock.release() - self.__thread = Thread(target=self._main_loop, name="job_queue") + self.__thread = Thread(target=self._main_loop, + name="Bot:{}:job_queue".format(self._dispatcher.bot.id)) self.__thread.start() self.logger.debug('%s thread started', self.__class__.__name__) else: diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 641c46f2794..e9f5c09ecc5 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -157,7 +157,8 @@ def __init__(self, self.__threads = [] def _init_thread(self, target, name, *args, **kwargs): - thr = Thread(target=self._thread_wrapper, name=name, args=(target,) + args, kwargs=kwargs) + thr = Thread(target=self._thread_wrapper, name="Bot:{}:{}".format(self.bot.id, name), + args=(target,) + args, kwargs=kwargs) thr.start() self.__threads.append(thr) diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index 73f1df3db13..87aa7a77ac1 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -384,6 +384,12 @@ def test_error_handler_context(self, cdp): sleep(.1) assert self.received == 'Unauthorized.' + def test_sensible_worker_thread_names(self, dp2): + thread_names = [thread.name for thread in getattr(dp2, '_Dispatcher__async_threads')] + print(thread_names) + for thread_name in thread_names: + assert thread_name.startswith("Bot:{}:worker:".format(dp2.bot.id)) + @pytest.mark.skipif(sys.version_info < (3, 0), reason='pytest fails this for no reason') def test_non_context_deprecation(self, dp): with pytest.warns(TelegramDeprecationWarning): diff --git a/tests/test_updater.py b/tests/test_updater.py index b8414cba695..1da48b89c68 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -111,8 +111,10 @@ def test(*args, **kwargs): # NOTE: Checking Updater.running is problematic because it is not set to False when there's # an unhandled exception. # TODO: We should have a way to poll Updater status and decide if it's running or not. - assert any('unhandled exception in updater' in rec.getMessage() for rec in - caplog.get_records('call')) + import pprint + pprint.pprint([rec.getMessage() for rec in caplog.get_records('call')]) + assert any('unhandled exception in Bot:{}:updater'.format(updater.bot.id) in + rec.getMessage() for rec in caplog.get_records('call')) @pytest.mark.parametrize(('error',), argvalues=[(RetryAfter(0.01),),