如何保持主动Python和QT线程的数量
我使用PYQT(附加在下面)进行代码,该代码应该创建线程,并计算单击按钮时有多少个线程。由于每个线程内部有一段时间。
但是,无论我单击多快,我只看到在终端上打印的数字1。我查看了线程文档和有关Active_count()的线程文档:
返回当前还活着的线程对象的数量。
因此,我想Qthreads不使用线程对象,而是其他一些实现。我没有发现在Qthread类文档中计算QThreads。最好的策略是什么?有什么比在列表中存储线程并将其删除列表更好的东西更好吗?
代码:
#!/usr/bin/python3
import sys, threading, time
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import QThread
threads=[]
class Example(QThread):
def run(self):
print(threading.active_count())
time.sleep(1)
def thread_creator():
new_thread=Example()
threads.append(new_thread)
new_thread.start()
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('QHBoxLayout')
layout = QHBoxLayout()
my_widget=QPushButton('Eat healthy!')
my_widget.clicked.connect(thread_creator)
layout.addWidget(my_widget)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
I did a code with PyQt (appended below) which is supposed to create threads and count how many of them are active when you click a button. Since there is a time.sleep(1) inside each thread, i should be able to click the button fast enough so that the active_threads count would increase before diminishing when each thread is finished.
However, no matter how fast i click the button i see only the number 1 printed on the terminal. I looked at the threading documentation and about active_count() it says:
Return the number of Thread objects currently alive.
So i suppose QThreads do not use thread objects, but some other implementation. I found nothing about counting QThreads in the QThread class documentation. What is the best strategy? Is there anything better than storing threads in a list and removing them of the list at the end?
Code:
#!/usr/bin/python3
import sys, threading, time
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import QThread
threads=[]
class Example(QThread):
def run(self):
print(threading.active_count())
time.sleep(1)
def thread_creator():
new_thread=Example()
threads.append(new_thread)
new_thread.start()
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('QHBoxLayout')
layout = QHBoxLayout()
my_widget=QPushButton('Eat healthy!')
my_widget.clicked.connect(thread_creator)
layout.addWidget(my_widget)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
线程
和qthread
只是用于管理 System 线程的包装对象。作为您引用的python文档,active_count
是pythonthread> thread
对象而不是系统线程的计数。显然,您的系统上可能还有许多其他的线程是由其他过程创建的,或者您程序使用的其他库(例如QT)。因此,您需要确定要计数哪个特定的线程子集。除了创建自己的QT外,还可以在内部创建线程,因此您可能不想计算这些线程...如果您只想计算自己启动的那些线程,那么您已经拥有
active_count
对于Python线程。对于QT,您可以使用 qthreadpool 。另一种可能性可能是使用中央 queue 所有线程都可以访问。Both
Thread
andQThread
are just wrapper objects for managing system threads. As the Python documentation you quoted states,active_count
is a count of PythonThread
objects, not system threads. Obviously there may be many other threads active on your system that were created by other processes, or by other libraries used by your program (such as Qt). So you need to decide which particular subset of threads to count. Qt may create threads internally in addition to the ones you created yourself, so you may not want to count those as well...If you want to count only those threads you started yourself, you already have
active_count
for the Python threads. For the Qt ones, you could use QThreadPool. Another possibility might be to use a central queue which could be accessed by all your threads.