app crashes when thread with api request is quitted
-
I want the thread to retrieve data from yfinance. but when the thread finishes its execution it crashes the application.
worker class :
class MyWorker(QObject): finished = Signal() def run(self): for i in range(15): stk = yf.Ticker('PNB.NS') hist = stk.history(period='1d', interval='1d') print(hist) time.sleep(1) self.finished.emit()
Thread creation:
self.worker = MyWorker() self.myThread = QThread() self.worker.moveToThread(self.myThread) self.worker.finished.connect(self.myThread.quit) self.worker.finished.connect(self.worker.deleteLater) self.myThread.started.connect(self.worker.run) self.myThread.finished.connect(self.myThread.deleteLater) self.myThread.start()
-
Hi and welcome to devnet,
Which version of PySide/PyQt ?
On which OS ?
How did you install it ?
Can you provide a complete minimal script that shows the behaviour ? -
@SGaist said in app crashes when thread with api request is quitted:
Can you provide a complete minimal script that shows the behaviour ?
@Shubham-Mandavkar
Can you complete your sample with (as little as possible!) code to make a complete program for people to see/run, i.e. you have shown some "thread creation" but we ant to see exactly how/where it is invoked in a full program, and inclass MyWorker
we don't know whatyf.Ticker()
&stk.history()
do, yet you tell us that withoutstk.history()
call it does not crash so presumably that is relevant.... -
@JonB yf is alias for yfinance and stk.history() returns the history of the stock from yfinance.
Below is the minimal code which you can run.# This Python file uses the following encoding: utf-8 import sys from PySide6.QtCore import QThread, Signal, QObject from PySide6.QtWidgets import QApplication, QMainWindow import time import yfinance as yf # Important: # You need to run the following command to generate the ui_form.py file # pyside6-uic form.ui -o ui_form.py, or # pyside2-uic form.ui -o ui_form.py class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) class MyWorker(QObject): finished = Signal() def run(self): for i in range(5): stk = yf.Ticker('PNB.NS') hist = stk.history(period='1d', interval='1d') print(hist) time.sleep(1) print('thread finished') self.finished.emit() if __name__ == "__main__": app = QApplication(sys.argv) widget = MainWindow() worker = MyWorker() myThread = QThread() worker.moveToThread(myThread) worker.finished.connect(myThread.quit) worker.finished.connect(worker.deleteLater) myThread.started.connect(worker.run) myThread.finished.connect(myThread.deleteLater) myThread.start() widget.show() sys.exit(app.exec())
gui does nothing. When thread completes its work the application automatically closes.
-
@Shubham-Mandavkar
Well, it still comes down top howyfinance
behaves, since that is causing the problem. And I note they sayyfinance offers a threaded and Pythonic way to download market data from Yahoo!Ⓡ finance.
So one might assume there is some issue between their "threaded" use and your use of
QThread
?Have you at least verified this works if you do not put it any
QThread
? And what does "crashes the application" mean, have you run it under a Python debugger or a system/C++ debugger to see if there is any cluse as to what might be the cause?