Python Query: Related to locking a resource in a multithreaded environment

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Aug 20 05:26:28 CEST 2008


En Tue, 19 Aug 2008 15:58:11 -0300, Nils Oliver Kröger <NO_Kroeger at gmx.de>  
escribi�:

> My approach what be to write one class for reading and writing to the
> configuration and make that class thread-safe using the RLock.
>
> Then you create one and only one instance of this class and let all your
> threads use (not import) that one instance.
>
> You could achieve that "one and only one" either by having a main
> program which creates the instance and pass the reference to each thread
> via its __init__ or you implement the Configuration class as a singleton
> like this:
>
> class Singleton(object): [...]
>Add your methods for accessing as instance methods to this class and get
> the same instance of this class in each thread by calling
> Singleton.getInstance().

Singleton.getInstance() is so "javaistic"... :)
Actually there is no need for all of that; just define the RLock at the  
global scope in a module.
Don't feel bad by using a "global" name - remember that a Python module  
acts as a named singleton. Defining an object at module scope is  
equivalent to defining an instance attribute in your Singleton class.

-- 
Gabriel Genellina




More information about the Python-list mailing list