How to solve the problem of bus charging with multiple threads in Python

Source: Internet
Author: User

Python multithreading has a wide range of applications. First, let's look at how to apply it. Let's take a look at the cases in life. I hope you will be inspired. Finally, simulate a Python multi-thread program for bus and subway IC Card Fare payment.

There are 10 card readers. Each card reader deducts a dollar each time and enters the General Ledger. Each card reader is charged 10000000 times a day. 100 original accounts. So the final general account should be 10000100. First, do not use mutex lock to lock and comment out the lock code) to see the consequences.

 
 
  1. import time,datetime  
  2. import threading  
  3. def worker(a_tid,a_account):  
  4. global g_mutex  
  5. print "Str " , a_tid, datetime.datetime.now()  
  6. for i in range(1000000):  
  7. #g_mutex.acquire()  
  8. a_account.deposite(1)  
  9. #g_mutex.release()  
  10. print "End " , a_tid , datetime.datetime.now()  
  11. class Account:  
  12. def __init__ (self, a_base ):  
  13. self.m_amount=a_base 
  14. def deposite(self,a_amount):  
  15. self.m_amount+=a_amount  
  16. def withdraw(self,a_amount):  
  17. self.m_amount-=a_amount 
  18. if __name__ == "__main__":  
  19. global g_mutex  
  20. count = 0 
  21. dstart = datetime.datetime.now()  
  22. print "Main Thread Start At: " , dstart  
  23. #init thread_pool  
  24. thread_pool = []  
  25. #init mutex  
  26. g_mutex = threading.Lock()  
  27. # init thread items  
  28. acc = Account(100)  
  29. for i in range(10):  
  30. th = threading.Thread(target=worker,args=(i,acc) ) ;  
  31. thread_pool.append(th)  
  32. # start threads one by one  
  33. for i in range(10):  
  34. thread_pool[i].start()  
  35. #collect all threads  
  36. for i in range(10):  
  37. threading.Thread.join(thread_pool[i])  
  38. dend = datetime.datetime.now()  
  39. print "count=",acc.m_amount  
  40. print "Main Thread End at: " ,dend , " time span " , 
    dend-dstart; 

The above is an introduction to the related Python multithreading technology.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.