Skip to content

Commit

Permalink
Merge pull request #21 from magfest/name_threads
Browse files Browse the repository at this point in the history
set thread names on startup
  • Loading branch information
EliAndrewC authored Aug 21, 2016
2 parents 435afc7 + 0c3b3d0 commit 9493061
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ env:
- TOX_ENV=py27
- TOX_ENV=py33
- TOX_ENV=py34
before_install:
- sudo apt-get -qq update
- sudo apt-get install -y build-essential libcap-dev
install:
- pip install tox
- pip install python-coveralls
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ paver==1.2.2
wheel==0.24.0
pip==1.5.6
sh==1.09
python-prctl==1.6.1
23 changes: 23 additions & 0 deletions sideboard/lib/_threads.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import unicode_literals
import time
import heapq
import prctl
import threading
import sys
from warnings import warn
from threading import Thread, Timer, Event, Lock

Expand All @@ -9,6 +12,26 @@
from sideboard.lib import log, on_startup, on_shutdown


# inject our own code at the start of every thread's start() method which sets the thread name via prctl().
# Python thread names will now be shown in external system tools like 'top', '/proc', etc.
def _thread_name_insert(self):
if self.name:
# linux doesn't allow thread names > 15 chars, and we ideally want to see the end of the name.
# attempt to shorten the name if we need to.
shorter_name = self.name if len(self.name) < 15 else self.name.replace('CP Server Thread', 'CPServ')
prctl.set_name(shorter_name)
threading.Thread._bootstrap_inner_original(self)
if sys.version_info[0] == 3:
threading.Thread._bootstrap_inner_original = threading.Thread._bootstrap_inner
threading.Thread._bootstrap_inner = _thread_name_insert
else:
threading.Thread._bootstrap_inner_original = threading.Thread._Thread__bootstrap
threading.Thread._Thread__bootstrap = _thread_name_insert

# set the name of the main thread
prctl.set_name('sideboard_main')


class DaemonTask(object):
def __init__(self, func, interval=0.1, threads=1, name=None):
self.lock = Lock()
Expand Down

0 comments on commit 9493061

Please sign in to comment.