Clean up on exit application

Heads up! You've already completed this tutorial.

Thomas_Parker | 2020-10-01 09:50:08 UTC | #1

Heya,

I have a quick question. I used your workermanager 6 example to build it into my application.

However I notice if I click the top right X to close the application, the threads just carry on running regardless!

What is the best way to go about closing everything and cleaning up on app exit?

A point in the right direction greatly appreciated! Thanks


Thomas_Parker | 2020-10-01 11:28:24 UTC | #2

PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.

Book Now 60 mins ($195)

I have just solved this with the following solution.

in my ui class (that inherites from a designer ui py file) I placed this function to catch the close event and issue a kill all to my worker managers (one set are providing a camera snapshot feed and the other is inference engines):

python
    def closeEvent(self, event):
        self.feed_workers.kill_all()
        self.engine_workers.kill_all()
        print('x button clicked')
        event.accept()

I then had an issue of it bugging when closing. I found out I had to store the app in a variable and pass it to sys exit (I still dont fully understand why! but it works):

Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

app.exec_() changed to:

python
app_ref = app.exec_()
sys.exit(app_ref)

Now when I click the top right X it kills the workers and exits error free.


Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

Clean up on exit application was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular https://www.martinfitzpatrick.com/browse/books/ on the subject.