Skip to content

Commit

Permalink
Add a prefix to threads (#1358)
Browse files Browse the repository at this point in the history
* Add a prefix to worker thread

This adds a prefix of `Bot:<id>:worker:` to the name of the worker threads.
Fixes #1332

* Also prefix other threads

* Fix test

* Fix test and remove helper method.
  • Loading branch information
Eldinnie committed Aug 23, 2019
1 parent d70577b commit edad6e8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion telegram/ext/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion telegram/ext/jobqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion telegram/ext/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),),
Expand Down

0 comments on commit edad6e8

Please sign in to comment.