PyQt5 在单独的 QThread 上运行 sklearn 计算 [英] PyQt5 run sklearn calculations on a separate QThread
问题描述
我正在创建一个 PyQt5 应用程序,并希望在单击按钮时在单独的线程中运行一些 ML 代码.我无法这样做.这是我的线程代码:
I am creating a PyQt5 application and want to run some ML code in a separate thread when a button is clicked. I am unable to do so. This is my threading code:
newthread = QtCore.QThread()
runthread = RunThread()
runthread.moveToThread(newthread)
runthread.finished.connect(newthread.quit)
newthread.started.connect(runthread.long_run)
newthread.start()
RunThread 类如下:
And the class RunThread is as follows:
class RunThread(QtCore.QObject):
finished = QtCore.pyqtSignal()
def __init__(self):
super().__init__()
@pyqtSlot()
def long_run(self):
#ML code called in the line below
simulation.start_simulation()
self.finished.emit()
正常运行是不行的.Pycharm 退出并出现以下错误:
Running it normally doesnt work. Pycharm quits with the following error:
process finished with exit code -1073740791 (0xc0000409)
在调试模式下运行它会抛出 sklearn 抛出的数千个警告说:
Running it in debug mode throws thousands of warnings thrown by sklearn saying:
Multiprocessing-backed parallel loops cannot be nested below threads,
setting n_jobs=1
代码最终会运行,但需要更多时间(至少 4 倍).
The code runs eventually, but it takes a lot more time (at least 4x times) to do so.
有人可以告诉我这里的问题是什么吗?
Could someone please let me know what the issue here is?
推荐答案
sklearn 的警告在这里非常明确.基本上,当从嵌套线程执行时,您不能将超参数 n_jobs
设置为大于 1 的任何值来训练 sklearn 模型.
The warning from sklearn is pretty explicit here. Basically you cannot train sklearn models with hyperparameter n_jobs
set to anything greater than 1 when executing from a nested thread.
话虽如此,如果没有看到 simulation.start_simulation()
内部发生了什么,就很难猜测.
That being said, without seeing what is going on inside of simulation.start_simulation()
it's hard to conjecture.
我最好的猜测是在 start_simulation()
中查找使用多个作业的任何内容,看看当您将其设置为 n_jobs=1
时会发生什么.
My best guess would be to look for anything in start_simulation()
that is using multiple jobs and see what happens when you set that to n_jobs=1
.
或者,您可以尝试将 ML 代码编写为独立脚本并使用 QProcess 执行.这可能允许您使用大于 1 的 n_jobs
.
Alternatively, you may try writing your ML code as a standalone script and execute it using QProcess. That may allow you to utilize n_jobs
greater than 1.
这篇关于PyQt5 在单独的 QThread 上运行 sklearn 计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!