How to terminate or stop Thread with python3

This is the place for queries that don't fit in any of the other categories.

How to terminate or stop Thread with python3

Postby matdoidep » Thu Apr 03, 2014 2:42 pm

Hi everyone,

I have a small App python.
I use Thread Object in https://docs.python.org/3.1/library/threading.html
But I don't know stop thread when started it.

Help me, Please!
Thanks!
Last edited by stranac on Thu Apr 03, 2014 2:50 pm, edited 1 time in total.
Reason: First post lock.
matdoidep
 
Posts: 1
Joined: Thu Apr 03, 2014 2:12 pm

Re: How to terminate or stop Thread with python3

Postby DrakeMagi » Thu Apr 03, 2014 6:47 pm

2 options

1. set .daemon to true closes when main program does

or
Code: Select all
import threading
import time

class Sample(threading.Thread):
   def __init__(self):
      threading.Thread.__init__(self)
      self.running = True
      self.data = 0
      
   def run(self):
      while self.running:
         self.data += 1
         time.sleep(1)
         
   def stop(self):
      self.running = False
      
mythread1 = Sample()
mythread1.start()

time.sleep(3)
mythread1.stop()

mythread1.join()
print(mythread1.data)
DrakeMagi
 
Posts: 89
Joined: Sun May 12, 2013 8:36 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests