This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

Concurrency Tips & Tricks

Use with statement to manage locks

Starting in Python 2.5, the with statement is a far easier way to manage locks:

 1 some_lock = threading.Lock()
 2 
 3 with some_lock:
 4  do_stuff_requiring_lock()

This is equivalent to:

 1 some_lock = threading.Lock()
 2 
 3 some_lock.acquire():
 4 try:
 5  do_stuff_requiring_lock()
 6 finally:
 7  some_lock.release()

Adjust checkinterval

Increasing the check interval may improve performance for CPU-bound multithreaded programs, at the cost of I/O responsiveness.


2026年02月14日 16:07

AltStyle によって変換されたページ (->オリジナル) /