Advertisement
Zakkq

Python Multi Threading Example

May 12th, 2014
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. import thread
  2. from time import sleep
  3.        
  4. def Thread1(threadName, delay):
  5.     x = 100
  6.     print("Threads started...\n")
  7.     while True:
  8.         x -= 1
  9.         if x >= 0:
  10.             sleep(1)
  11.             print("Thread1[" + str(x) +"]\n")
  12.         else:
  13.             sleep(1)
  14.             print("Thread1 - Done")
  15.             break
  16.  
  17. def Thread2(threadName, delay):
  18.     y = 0
  19.     sleep(3)
  20.     while True:
  21.         y += 1
  22.         if y <= 100:
  23.             sleep(1)
  24.             print("Thread2[" + str(y) + "]\n")
  25.         else:
  26.             sleep(1)
  27.             print("Thread2 - Done")
  28.             break
  29.        
  30. print("Starting up Threads...")
  31. sleep(1)
  32.  
  33. thread.start_new_thread( Thread1, ("Thread-1", 1) )
  34. thread.start_new_thread( Thread2, ("Thread-2", 1) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement