【发布时间】:2009-08-16 14:06:40
【问题描述】:
我正在使用 Wing IDE 调试一个多线程 Python 程序。
当我按下暂停按钮时,它只暂停一个线程。我已经尝试了十次,它总是暂停同一个线程,在我的例子中称为“ThreadTimer Thread”,而其他线程继续运行。我想暂停这些其他线程,以便我可以与它们一起工作。我该怎么做?
【问题讨论】:
标签: python multithreading debugging ide
我正在使用 Wing IDE 调试一个多线程 Python 程序。
当我按下暂停按钮时,它只暂停一个线程。我已经尝试了十次,它总是暂停同一个线程,在我的例子中称为“ThreadTimer Thread”,而其他线程继续运行。我想暂停这些其他线程,以便我可以与它们一起工作。我该怎么做?
【问题讨论】:
标签: python multithreading debugging ide
我不知道 Wing IDE 是否可以进行多线程调试。
但是您可能对具有此功能的Winpdb 感兴趣
【讨论】:
根据the docs,所有运行 Python 代码的线程都将停止(默认情况下,除非您不打算实现不同的效果)。您看到的线程是否没有停止运行非 Python 代码(I/O,比如说:这会产生自己的问题),或者您是否正在做其他事情而不是在原始安装中运行而没有文档描述的仅暂停的调整一些线程...?
【讨论】:
我只是在创建线程时为其命名。
示例线程:
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()
然后你可以随时从线程内部访问名称
print(threading.currentThread())
sys.stdout.flush() #this is essential to print before the thread is completed
您还可以列出具有特定名称的所有线程
for at in threading.enumerate():
if at.getName().split(' ')[0] == 'Connection':
print('\t'+at.getName())
可以对进程做类似的事情。
示例流程:
import multiprocessing
process1 = multiprocessing.Process(target=function,
name='ListenerProcess',
args=(queue, connections, known_clients, readouts),
daemon=True)
process1.start()
使用进程会更好,因为您可以从外部通过其名称终止特定进程
for child in multiprocessing.active_children():
if child.name == 'ListenerProcess':
print('Terminating:', child, 'PID:', child.pid)
child.terminate()
child.join()
【讨论】: