User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 374,506 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,816 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
May 9th, 2008
Views: 1,115
For more information on threading read this excellent 4 page tutorial:
http://www.devshed.com/c/a/Python/Ba...ing-in-Python/
python Syntax
  1. # counterTest.py, a1eio
  2.  
  3. import threading
  4.  
  5. # create a thread object that will do the counting in a separate thread
  6. class Counter(threading.Thread):
  7. def __init__(self, value, increment):
  8. threading.Thread.__init__(self) # init the thread
  9. self.value = value # initial value
  10. self.increment = increment # amount to increment
  11. self.alive = False # controls the while loop in the run command
  12.  
  13. def run(self): # this is the main function that will run in a separate thread
  14. self.alive = True
  15. while self.alive:
  16. self.value += self.increment # do the counting
  17.  
  18. def peek(self): # return the current value
  19. return self.value
  20.  
  21. def finish(self): # close the thread, return final value
  22. self.alive = False # stop the while loop in 'run'
  23. return self.value # return value
  24.  
  25. def main():
  26. # create separate instances of the counter
  27. counterA = Counter(1, 1) #initial value, increment
  28. counterB = Counter(1, 2) #initial value, increment
  29. counterC = Counter(1, 3) #initial value, increment
  30.  
  31. # start each counter
  32. counterA.start()
  33. counterB.start()
  34. counterC.start()
  35.  
  36. print "Enter A, B, or C to view counters\nEnter Q to quit"
  37. while True:
  38. Input = raw_input("> ").upper() # get input
  39.  
  40. if Input == 'A':
  41. print 'CounterA: ', counterA.peek() # print counterA's value
  42. elif Input == 'B':
  43. print 'CounterB: ', counterB.peek() # print counterB's value
  44. elif Input == 'C':
  45. print 'CounterC: ', counterC.peek() # print counterC's value
  46.  
  47. elif Input == 'Q': # if user entered q or Q, program quits
  48. # call the function that finishes each counter and print the returned value
  49. print 'CounterA: ', counterA.finish()
  50. print 'CounterB: ', counterB.finish()
  51. print 'CounterC: ', counterC.finish()
  52. return # exit the Main function
  53.  
  54. else:
  55. pass
  56.  
  57. if __name__ == '__main__':
  58. main()
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 2:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC