6

I initialise my logger in main.py like so:

logging.basicConfig(level=logging.DEBUG, format='%(process)d - %(name)s - %(levelname)s - %(message)s')

Then in each module at the top I have:

logger = logging.getLogger(__name__)

When I use the logger in multiple threads, the process id shown is the same (the parent id), the thread ID is different if included. However, when I use the top commandline tool in linux, I see different PIDs for each thread.

How can I show the child PID when it uses the logger? I need to narrow down where an issue is occuring and I can't from just knowing the module name.

For example, when I have my main thread spawn another thread, top shows these two entries:

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND    
 9686 jm354     20   0  528608  17584   9192 S   0.0   0.2   0:00.07 python                                                                                                          
 9690 jm354     20   0  528608  17584   9192 S   0.0   0.2   0:00.01 python    

But the logs only shows the PID 9686, despite logger.info being used in different threads.

I'm using the multiprocessing Queue and threading.Thread modules

import logging
import threading
from multiprocessing import Queue
Jordan Mackie
  • 2,264
  • 4
  • 25
  • 45

1 Answers1

4

Well, over here they discuss a solution using ctypes and Linux-specific code. This is not portable but could do for debugging.

rfindeis
  • 461
  • 3
  • 15