Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. app crashes when thread with api request is quitted

app crashes when thread with api request is quitted

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 3 Posters 1.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shubham Mandavkar
    wrote on last edited by
    #1

    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()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S JonBJ 2 Replies Last reply
      0
      • SGaistS SGaist

        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 ?

        S Offline
        S Offline
        Shubham Mandavkar
        wrote on last edited by
        #3

        @SGaist I am using PySide6 on window11 and installed PySide6 using python pip.
        And when I comment the stk.history() line application works fine.

        1 Reply Last reply
        0
        • SGaistS SGaist

          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 ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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 in class MyWorker we don't know what yf.Ticker() & stk.history() do, yet you tell us that without stk.history() call it does not crash so presumably that is relevant....

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @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 in class MyWorker we don't know what yf.Ticker() & stk.history() do, yet you tell us that without stk.history() call it does not crash so presumably that is relevant....

            S Offline
            S Offline
            Shubham Mandavkar
            wrote on last edited by
            #5

            @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.

            JonBJ 1 Reply Last reply
            0
            • S Shubham Mandavkar

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Shubham-Mandavkar
              Well, it still comes down top how yfinance behaves, since that is causing the problem. And I note they say

              yfinance 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?

              SGaistS 1 Reply Last reply
              0
              • JonBJ JonB

                @Shubham-Mandavkar
                Well, it still comes down top how yfinance behaves, since that is causing the problem. And I note they say

                yfinance 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?

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Can you share how you created your Python environment ?

                On macOS, with:

                • Python 3.12.0
                • PySide6 6.6.1
                • yfinance 0.2.33

                No crash happened.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved