简体   繁体   中英

Debugging multi-threaded Python with Wing IDE

I'm debugging a multi-threaded Python program with Wing IDE.

When I press the pause button, it pauses only one thread. I've tried it ten times and it always pauses the same thread, in my case called "ThreadTimer Thread", while the other threads keep on running. I want to pause these other threads so I could step with them. How do I do that?

I don't know if multi-thread debugging is possible with Wing IDE.

However you maybe interested in Winpdb which has this capability

Per the docs , all threads that are running Python code are stopped (by default, ie, unless you're going out of the way to achieve a different effect). Are the threads that you see as not getting stopped running non-Python code (I/O, say: that gives its own issues), or are you doing something else than running in a pristine install without the tweaks the docs describe to only pause some of the threads...?

I just name my threads when I create them.

Example thread:

import threading
from threading import Thread

#...
client_address = '192.168.0.2'
#...

thread1 = Thread(target=thread_for_receiving_data,
                         args=(connection, q, rdots),
                         name='Connection with '+client_address,
                         daemon=True)
thread1.start()

Then you can always access the name from inside the thread

print(threading.currentThread())
sys.stdout.flush() #this is essential to print before the thread is completed

You can also list all threads with a specific name

for at in threading.enumerate():
    if at.getName().split(' ')[0] == 'Connection':
        print('\t'+at.getName())

A similar thing can be done to a process.

Example process:

import multiprocessing


process1 = multiprocessing.Process(target=function,
                         name='ListenerProcess',
                         args=(queue, connections, known_clients, readouts),
                         daemon=True)
process1.start()

With processes it is even better as you can terminate a specific process by its name from outside of it

for child in multiprocessing.active_children():
    if child.name == 'ListenerProcess':
        print('Terminating:', child, 'PID:', child.pid)
        child.terminate()
        child.join()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM