Sensible small Python example using locks with threads
I am searching for a sensible (i.e. it should really be useful in terms of efficiency) example using python threads & locks. I know many standard small examples but they are all missing at least one of the properties "small", "sensible" or "using locks" -- for example:
- Testing a list of URLs to check availability (sensible, small, but uses no locks)
- Implementing a few clients (one per thread) and a server holding a variable (or bank account or something alike) -- small, uses locks, but not sensible (in practice开发者_如何转开发 there are better ways to implement this).
- Parallelzing a simple algorithm via threads (like the summation of a large list) -- small, but both not sensible (cause you wouldnt parallelize via threads) and not using locks.
Doug Hellmann's page is always a good address to get some examples:
For threading in general: http://www.doughellmann.com/PyMOTW/threading/index.html
Or if you may prefer multiprocessing (e.g. the GIL hits you, or you like to distribute your load over multiple processors): http://www.doughellmann.com/PyMOTW/multiprocessing/index.html
Python's Queue module is a great example of small, yet, synchronized efficient implementation of a queue for the producer/consumer problem.
精彩评论