退出 1000 个 QThread - pyqt5
quit 1000 of QThread - pyqt5
我有 python
gui 应用程序使用 Qt
我正在使用 pyqt5
应用程序应创建 1000 个或更多 Qthreads
,每个线程将使用 pycurl
打开外部 URL
我正在使用此代码打开线程:
self.__threads = []
# workerClass thread
for i in range(1000):
workerInstance = workerClass()
workerInstance.sig.connect(self.ui.log.append)
thread = QThread()
self.__threads.append((thread, workerInstance))
workerInstance.moveToThread(thread)
thread.started.connect(workerInstance.worker_func)
thread.start()
WorkerClass 将使用简单的 pycurl
访问外部 URL 并发出信号以在日志中添加一些信息,代码如下:
class workerClass(QObject):
x = 1
sig = pyqtSignal(str)
def __init__(self, linksInstance, timeOut):
super().__init__()
self.linksInstance = linksInstance
self.timeOut = timeOut
@pyqtSlot()
def worker_func(self):
while self.x:
# run the pycurl code
self.sig.emit('success!') ## emit success message
现在的问题是停止这一切,我使用下面的代码作为停止按钮:
def stop_func(self):
workerClass.x = 0
if self.__threads:
for thread, worker in self.__threads:
thread.quit()
thread.wait()
将 workerClass.x
更改为 0 将停止 class workerClass
中的 while self.x
并退出并等待应该关闭所有线程
所有这一切都正常工作,但只有在线程数量很少的情况下,10 或可能是 100
但是如果我调用 1000 个线程,这会花费很多时间,我等了 10 分钟但它没有停止(意味着线程没有终止),但是 pycurl
超时只有 15 秒
我也用过:self.thread.exit()
self.thread.quit()
self.thread.terminate()
但是没有区别
如果有人对此有任何想法,那就太好了:)
经过一些尝试,我删除了 pycurl 代码并测试以启动 10k QThreads 并停止它,这完美无误地工作。所以我定义错误与pycurl代码有关。
根据问题评论中ekhumoro的建议,我可能会尝试使用pycurl multi
但现在我将这一行添加到 pycurl 代码中:
c.setopt(c.TIMEOUT, self.timeOut)
我只用过这个:
c.setopt (c.CONNECTTIMEOUT, self.timeOut)
所以现在这两个参数都可以正常工作,启动和停止批量 qthreads
我有 python
gui 应用程序使用 Qt
我正在使用 pyqt5
应用程序应创建 1000 个或更多 Qthreads
,每个线程将使用 pycurl
打开外部 URL
我正在使用此代码打开线程:
self.__threads = []
# workerClass thread
for i in range(1000):
workerInstance = workerClass()
workerInstance.sig.connect(self.ui.log.append)
thread = QThread()
self.__threads.append((thread, workerInstance))
workerInstance.moveToThread(thread)
thread.started.connect(workerInstance.worker_func)
thread.start()
WorkerClass 将使用简单的 pycurl
访问外部 URL 并发出信号以在日志中添加一些信息,代码如下:
class workerClass(QObject):
x = 1
sig = pyqtSignal(str)
def __init__(self, linksInstance, timeOut):
super().__init__()
self.linksInstance = linksInstance
self.timeOut = timeOut
@pyqtSlot()
def worker_func(self):
while self.x:
# run the pycurl code
self.sig.emit('success!') ## emit success message
现在的问题是停止这一切,我使用下面的代码作为停止按钮:
def stop_func(self):
workerClass.x = 0
if self.__threads:
for thread, worker in self.__threads:
thread.quit()
thread.wait()
将 workerClass.x
更改为 0 将停止 class workerClass
中的 while self.x
并退出并等待应该关闭所有线程
所有这一切都正常工作,但只有在线程数量很少的情况下,10 或可能是 100
但是如果我调用 1000 个线程,这会花费很多时间,我等了 10 分钟但它没有停止(意味着线程没有终止),但是 pycurl
超时只有 15 秒
我也用过:self.thread.exit()
self.thread.quit()
self.thread.terminate()
但是没有区别
如果有人对此有任何想法,那就太好了:)
经过一些尝试,我删除了 pycurl 代码并测试以启动 10k QThreads 并停止它,这完美无误地工作。所以我定义错误与pycurl代码有关。
根据问题评论中ekhumoro的建议,我可能会尝试使用pycurl multi
但现在我将这一行添加到 pycurl 代码中:
c.setopt(c.TIMEOUT, self.timeOut)
我只用过这个:
c.setopt (c.CONNECTTIMEOUT, self.timeOut)
所以现在这两个参数都可以正常工作,启动和停止批量 qthreads