From 4536e44b8c3004710a7f3968b8cbc462df70f4d2 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Thu, 6 Jul 2017 19:35:40 +0200 Subject: [PATCH] Thread names propagate to system - the thread names should now show up in the monitoring tools of operating systems (tested on linux) --- src/bitmessagemain.py | 6 ++++-- src/helper_threading.py | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 88fa3c1d..b142676b 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -63,7 +63,7 @@ from network.downloadthread import DownloadThread # Helper Functions import helper_bootstrap import helper_generic -from helper_threading import * +import helper_threading def connectToStream(streamNumber): @@ -153,7 +153,7 @@ def _fixSocket(): socket.IPV6_V6ONLY = 27 # This thread, of which there is only one, runs the API. -class singleAPI(threading.Thread, StoppableThread): +class singleAPI(threading.Thread, helper_threading.StoppableThread): def __init__(self): threading.Thread.__init__(self, name="singleAPI") self.initStop() @@ -204,6 +204,8 @@ class Main: self.setSignalHandler() + helper_threading.set_thread_name("MainThread") + helper_bootstrap.knownNodes() # Start the address generation thread addressGeneratorThread = addressGenerator() diff --git a/src/helper_threading.py b/src/helper_threading.py index 599d297d..cd51557f 100644 --- a/src/helper_threading.py +++ b/src/helper_threading.py @@ -1,4 +1,17 @@ import threading +try: + import prctl + def set_thread_name(name): prctl.set_name(name) + + def _thread_name_hack(self): + set_thread_name(self.name) + threading.Thread.__bootstrap_original__(self) + + threading.Thread.__bootstrap_original__ = threading.Thread._Thread__bootstrap + threading.Thread._Thread__bootstrap = _thread_name_hack +except ImportError: + log('WARN: prctl module is not installed. You will not be able to see thread names') + def set_thread_name(name): pass class StoppableThread(object): def initStop(self): @@ -7,4 +20,4 @@ class StoppableThread(object): def stopThread(self): self._stopped = True - self.stop.set() \ No newline at end of file + self.stop.set()