Skip to content

Commit

Permalink
Add custom thread names
Browse files Browse the repository at this point in the history
Make debugging a bit easier by labelling each thread with its task name.
Set the "show custom thread names" option in htop to see the results.
  • Loading branch information
waveform80 committed Jun 10, 2019
1 parent 7e7309b commit 803db28
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions piwheels/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,24 @@
:members:
"""

import ctypes as ct
import logging
from threading import Thread
from fnmatch import fnmatch

from . import transport, protocols


# Grab the prctl(2) function from libc; the prototype is actually a lie, but
# it's correct for our purposes
libc = ct.CDLL("libc.so.6")
prctl = libc.prctl
prctl.argtypes = [ct.c_int, ct.c_char_p, ct.c_ulong, ct.c_ulong, ct.c_ulong]
prctl.restype = ct.c_int
# From include/linux/prctl.h
PR_SET_NAME = 15


class TaskQuit(Exception):
"""
Exception raised when the "QUIT" message is received by the internal
Expand Down Expand Up @@ -220,8 +231,13 @@ def run(self):
any finalization required.
"""
self.logger.info('starting')
# Set the thread's name to self.name (well, the first 15 chars anyway);
# this helps with debugging as htop and top can show custom thread
# names (given the right settings)
prctl(PR_SET_NAME, self.name.encode('ascii')[:15], 0, 0, 0)
try:
self.once()
self.logger.info('started')
while True:
self.loop()
self.poll()
Expand Down

0 comments on commit 803db28

Please sign in to comment.