How can I pause all other threads in Python?

I am doing a "python debug mapper" that shows a 'snapshot' current python run

Currently, I need to know how to pause every other threads so that 'capture' does not execute while other threads are executing.

Is there any way to do:

  • PauseOtherThreads ();
  • ResumeOtherThreads ();

Thanks.

ps: should I make any changes to the code working with Celery and Django?

+4
source share
2 answers

Depending on whether you just want to keep track of one thread while other threads are running, or if you want to stop other threads, I can think of two solutions. If other threads should run without tracing, just ask the trace command to first check the current thread id and perform a trace operation if the thread is the one you are interested in:

 def dotrace(): if tracing and threading.current_thread() == the_traced_thread: ... do the tracing ... 

If instead other threads should stop during tracking, you can make your trace work as a stop for other threads by adding something like:

 def dotrace(): while tracing and threading.current_thread() != the_traced_thread: time.sleep(0.01) if tracing and threading.current_thread() == the_traced_thread: ... do the tracing ... 

Of course, only the last operations will work as a stop in the latter case, so that other threads can continue to work until they end, or they will not trace anything.

Basically, you will only stop other threads that you control, not all other threads. I would say that this is good, because it increases the likelihood that the program will remain functional (some of the libraries and frameworks used may need other threads for the thread that actually works for it), but, of course, YMMV.

+1
source

You can use sys.setcheckinterval(somebignumber)

  setcheckinterval (...)
     setcheckinterval (n)

     Tell the Python interpreter to check for asynchronous events every
     n instructions.  This also affects how often thread switches occur.
0
source

All Articles