Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a prefix to worker thread #1358

Merged
merged 4 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 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 Expand Up @@ -209,6 +210,13 @@ def _pooled(self):
'DispatcherHandlerStop is not supported with async functions; func: %s',
promise.pooled_function.__name__)

# This method purely exsists for testing purposes.
def _get_thread_names(self):
if self.__async_threads:
return (x.name for x in self.__async_threads)
else:
return None

def run_async(self, func, *args, **kwargs):
"""Queue a function (with given args/kwargs) to be run asynchronously.

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
5 changes: 5 additions & 0 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ def test_error_handler_context(self, cdp):
sleep(.1)
assert self.received == 'Unauthorized.'

def test_sensible_worker_thread_names(self, dp2):
thread_names = [name for name in dp2._get_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