title: Trace subprocesses and threads in Python
body: <p>I have a Python script running in Linux (Ubuntu 18.04) that creates processes using <a href="https://docs.python.org/3/library/multiprocessing.html" rel="nofollow noreferrer">multiprocessing</a> library, assigning to them functions to be executed. The problem is that one of these functions gets stuck, and I want to know where it happens. I tried using <code>python -m trace -l myscript.py</code>, but it cannot show the functions that are executed in the subprocesses. Although the script below is not the one I am using, this situation also occurs when trying to trace its functions.</p>
  
  <pre><code>import multiprocessing
  
  def print_hello():
      print('hello world')
  
  def print_hello_inside_trace():
      print('trace: hello world')
  
  def trace_function():
      print_hello_inside_trace()
  
  if __name__ == '__main__':
      print_hello()
      process = multiprocessing.Process(target=trace_function)
      process.start()
      process.join()
  </code></pre>
  
  <p>So, my question is: is there any way to trace the code executing inside Python subprocesses? Also, as curiosity, is it possible to trace Python threads?</p>