Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stop multiple threads
#5
Here it is using multiprocessing if it helps. https://pymotw.com/3/multiprocessing/index.html
import sys
if 3 == sys.version_info[0]: ## 3.X is default if dual system
    import tkinter as tk     ## Python 3.x
else:
    import Tkinter as tk     ## Python 2.x

import time
from multiprocessing import Process

class TestClass():
    def test_f(self, spaces=1):
        ctr = spaces
        while True:
            ctr += 1
            print(" "*spaces, ctr)
            time.sleep(1.0)

    def tk_quit(self):
        root=tk.Tk()
        tk.Button(root, text="Quit", command=root.quit,
                  width=10).grid()
        root.mainloop()

if __name__ == '__main__':
     ## run functions in the background
     CT=TestClass()
     p = Process(target=CT.test_f)
     p.start()
     ## run same function in another process to 
     ## simulate 2 different processes
     p2 = Process(target=CT.test_f, args=(10,))
     p2.start()

     CT.tk_quit()
     print("terminate process")
     if p.is_alive():
         p.terminate()
         p.join()
     if p2.is_alive():
         p2.terminate()
         p2.join() 
Reply


Messages In This Thread
stop multiple threads - by jeuvrey - Nov-13-2018, 03:53 PM
RE: stop multiple threads - by Gribouillis - Nov-13-2018, 04:23 PM
RE: stop multiple threads - by jeuvrey - Nov-14-2018, 09:29 AM
RE: stop multiple threads - by Gribouillis - Nov-14-2018, 11:41 AM
RE: stop multiple threads - by woooee - Nov-14-2018, 05:41 PM
RE: stop multiple threads - by jeuvrey - Nov-15-2018, 01:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What to do to stop all these threads? Valjean 3 2,048 Oct-02-2023, 01:50 PM
Last Post: deanhystad
  Trying to separate a loop across multiple threads stylingpat 0 2,216 May-05-2021, 05:21 PM
Last Post: stylingpat
  Get average of multiple threads Contra_Boy 1 23,147 May-05-2020, 04:51 PM
Last Post: deanhystad
  Quitting multiple threads MuntyScruntfundle 3 3,544 Oct-17-2018, 05:14 AM
Last Post: volcano63
  How to run multiple threads properly cyberion1985 6 8,402 Dec-29-2017, 09:45 AM
Last Post: cyberion1985

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020