+ 9

[open questions] Python, threading, how to stop a second thread?

I have this code here: https://code.sololearn.com/cjJOE24e49E9/?ref=app I have a first thread which is started. then it runs for 5 seconds. when it stops, a second thread 'meier' is started. after 5 seconds i want to stop 'meier' in the same way i stopped the first thread before, but 'meier' runs despite calling t2.stop(). What do i have to do that thread 'meier' stops after 5 seconds also? Does this have to do something with the instruction class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None) out of the Python3 documentation? I don't understand how I have to implement the method 'meier()' with the 'target'-parameter. Can anybody explain and show me how to do this in my code? I am also not getting enlightened by http://sebastiandahlgren.se/2014/06/27/running-a-method-as-a-background-thread-in-JUMP_LINK__&&__python__&&__JUMP_LINK/

13th Jan 2020, 6:18 AM
Jan Markus
5 odpowiedzi
+ 4
t2.meier() is a normal method, it does not count as a new thread. You will need to make it a thread first, before you can run it separatly. Otherwise, it will run like any internal function, and the loop will never end.
13th Jan 2020, 6:45 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Thank you Aymane Boukrouh [INACTIVE] ! But how do I have to modify the code that 'meier()' can be run as a second thread and be stopped independently from another thread? I have a couple more questions: Question1: Do all threads have to be located in the 'run()'-method of the threading-module? Question2: And how do a I have to apply the 'target'-parameter of threading.Thread(...)? Does 'target' make it possible to have a thread in another method than the 'run()'-method?
13th Jan 2020, 7:33 AM
Jan Markus
+ 4
Jan Markus well unfortunately again, I haven't worked with tkinter, but I have a lot of experience with Qt5. GUIs usually handle multiple threads for themselves, everything you write should be inside the mainloop. If you mean running multiple while loops and for loops inside, then yes this will definitely stop or at least slow down your GUI. But, I believe that GUIs have their own internal methods for that. Since you mentioned time, Qt5 already has a timer, called QTimer. This timer is independent, and will keep updating in the background, while running a function or method everytime it reaches timeout. The point is, before trying to learn threading module, make sure there aren't any built-in method of tkinter that do that for you, it is very important to use them, instead of threading.
13th Jan 2020, 10:37 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Jan Markus I haven't worked with the threading module much before, so I cannot say anything about both of your questions, this is just my guess: The run() method is probably already defined, with the purpose of overriding it. When you redefine it, the start() method executes what the run() method defines, so I don't think ou can change that, unless you override the start() method itself. I guess this answers both of your questions, but again, I am not sure of it, and this is just an assumption I made after working with other similar modules that allow overriding, and not this specific one. Can you tell me what you want to do tho ?
13th Jan 2020, 10:10 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Hello Aymane Boukrouh [INACTIVE] , the reason why I came to the topic of 'threading' comes from the use of Tkinter. After having designed the GUI then you have to execute the mainloop() method which is in fact an endless loop. While a GUI-program is running I want to have some areas of the GUI be updated always. E.g. a clock which has to be updated every second, or in the background I want to monitor changes in a database or on a website in fixed time intervals, and I thought that doing this with threads would make this task easier. I know this concept through dealing with PIC microcontrollers in Assembler on which you could aktivate hardware interrupts for doing several things (nearly) in parallel.
13th Jan 2020, 10:29 AM
Jan Markus