2

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.
2
  • 2
    Perhaps 'sensible' and 'using locks' are mutually exclusive? Commented Mar 9, 2011 at 11:47
  • This is a great question because it points up the essential issue: Threads are not often helpful. Sensible examples are hard to find because multi-processing and message queues (or pipes) is almost always a better approach. Commented Mar 9, 2011 at 12:58

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

Python's Queue module is a great example of small, yet, synchronized efficient implementation of a queue for the producer/consumer problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.