Python multithreading module threading example code

From , 5 Years ago, written in Python, viewed 171 times.
URL https://pastebin.vip/view/110e7d18
  1. #!/usr/bin/python
  2.  
  3. import threading
  4. import time
  5.  
  6. exitFlag = 0
  7.  
  8. class myThread (threading.Thread):
  9.     def __init__(self, threadID, name, counter):
  10.         threading.Thread.__init__(self)
  11.         self.threadID = threadID
  12.         self.name = name
  13.         self.counter = counter
  14.     def run(self):
  15.         print "Starting " + self.name
  16.         print_time(self.name, self.counter, 5)
  17.         print "Exiting " + self.name
  18.  
  19. def print_time(threadName, delay, counter):
  20.     while counter:
  21.         if exitFlag:
  22.             thread.exit()
  23.         time.sleep(delay)
  24.         print "%s: %s" % (threadName, time.ctime(time.time()))
  25.         counter -= 1
  26.  
  27. # Create new threads
  28. thread1 = myThread(1, "Thread-1", 1)
  29. thread2 = myThread(2, "Thread-2", 2)
  30.  
  31. # Start new Threads
  32. thread1.start()
  33. thread2.start()
  34.  
  35. print "Exiting Main Thread"
  36.  
  37.  
  38. #//python/8381

Reply to "Python multithreading module threading example code"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website