Repository: python-telegram-bot/python-telegram-bot

Issue:
give threads a name prefix
Just an idea if **_python-telegram-bot_** is part of a bigger program: 

It would be a nice thing to improve bug finding in bigger programms of which python-telegram-bot is a part of if one could name the thread. Instead of names like

```
dispatcher
updater
84a23bc3-ec23-4aeb-be53-f24e7af82544_0
84a23bc3-ec23-4aeb-be53-f24e7af82544_1
84a23bc3-ec23-4aeb-be53-f24e7af82544_2
84a23bc3-ec23-4aeb-be53-f24e7af82544_3
```

I would rather like to see
```
telegram_dispatcher
telegram_updater
telegram_worker_0
telegram_worker_1
telegram_worker_2
telegram_worker_3
```
Since any thread has an id anyway, this should not be too difficult.
So instead using uuid4 to name a worker, just give a custom param of ``name_prefix='telegram'`` and ``worker_name='worker'``

Thanks in advance!




Discussion:
This looks like it should be doable, we'll look into it and get back to you.
(note that it might be a while as we're right now pretty focused on getting V12 out asap)
Ok, thanks for letting me know, I am looking forward to this improvement.
@bmxp good find. Would you be comfortable to PR this yourself?
I would love to but my expertise is not good enough to provide a PR and I am not very good with API creating thus I'd rather step aside and let someone else make a superior solution.
To give some more background: We have an Open Source Project [SmartHomeNG](https://github.com/smarthomeNG/smarthome) and have a plugin which uses the library. To distinguish the threads it will help to have them named.
![image](https://user-images.githubusercontent.com/3002409/52851836-68933f00-3117-11e9-980d-11b49a4ab6b1.png)


Resolving patch:
diff --git a/telegram/ext/dispatcher.py b/telegram/ext/dispatcher.py
--- 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
--- 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
--- 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)
 


Test patch:
diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py
--- 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
--- 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),),