In this post, we will see how to resolve How to stop a thread that has a blocking function from easygui in python
Question:
Context
I am using the easygui library to make a simple interface for my program.
In the program, I need to start a process that takes time and the user can cancel it if he is tired of waiting.
Code
Here is a minimal example code:
The goal
I want to close the msgbox if the connection is successful and the user never clicked ok to cancel the connection.
The problem
The problem is that if the user never clicks ok, the msgbox will never return and the code will never reach any line after the msgbox line.
So I can’t even add a flag to the end_connection function because there will never be a chance for the function to check it.
Main question
How can I kill this monitor_thread “gracefully”?
Or, is there another way to achieve the desired result?
Outro
Let me know if you need more code or information to understand something. The actual code has a lot of unnecessary things that may draw attention from the actual problem.
Best Answer:
You could use themultiprocessing
module instead of threading
because it allow you to terminate a process whereas thread dont do it properly.Here is the modified code:
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com