-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I don't think the global interpreter lock is what you need ... read here
for reference:

http://docs.python.org/api/threads.html

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):
        """
        A simple example implementing the singleton design pattern in
        python
        """
        #we need two class attributes (which translate to static        
                attributes in java)
        __lock = Lock() #a lock for thread safety
        __instance = None #and to remember the one instance

        #first of all: make pythons usual way of creating objects       
        # unusable because we cannot just hide them as one would in java        
# or C++
        def __new__(cls, *args, **kwargs):
                pass

        def __init__(self):
                pass

        @classmethod
        def getInstance(cls, *args, **kwargs):
        """
        The famous gatInstance method which resturns the one instance of        
our
        class.

        params:
        cls - reference to the class
        *args - the tuple of arguments paassed by position
        **kwargs - the dictionary of arguments passed by keyword
        """
        #enter critical section
        cls.__lock.acquire()
        try:
                if cls.__instance is None:
                        #use the superclasses __new__ for creation
                        cls.__instance = object.__new__(cls, *args,             
                                                        **kwargs)

                        #place initialisation code (everything which            
                        #would usually
happen in __init__) here
                        cls.__instance.SomeInt = 1
        finally:
                #leave critical section
                cls.__lock.release()

        return cls.__instance


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().

Hope that helps

Regards

Nils

tarun schrieb:
> I think I need something called global interpreter lock which is
> accessible to all the threads. But I am not sure how to implement this.
> 
>  
> On Tue, Aug 19, 2008 at 11:28 AM, tarun <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
> 
>     Hello All,
>      
>     I've a configuration.ini file. This particular can be accessed by
>     several threads of my application. Each thread can read/write
>     configuration parameters from/to the configuration.ini file. I am
>     using threading (Rlock) to lock the resource (configuration.ini
>     file) while accessing it from any thread. I use the file available
>     at http://www.voidspace.org.uk/python/configobj.html for read/write.
>      
>     I did the following in one of my files:
>      
>     import threading
>     class Synchronization:
>       def __init__(self):
>         self.mutex = threading.RLock()
>      
>     Now this I can create instances of the class Synchronization and use
>     acquire/release to work on the shared resource. But every thread
>     would need to import this class and hence each thread would create a
>     separate instance of the class. This means several lock variables.
>     But I think we need a global lock flag that is accessible across all
>     the threads.
>      
>     How do i do this?
>      
>     Please let me know ASAP.
>      
>     Thanks In Advance,
>     Tarun
> 
> 
> 
> ------------------------------------------------------------------------
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
-----BEGIN PGP SIGNATURE-----

iD8DBQFIqxfCzvGJy8WEGTcRApe+AJ9MNqWI9FOsJIKuTKxy8ZNSGYTy2gCdHtGc
clDPMMAPRoIxsBvVm4ygi6U=
=vIPW
-----END PGP SIGNATURE-----
begin:vcard
fn;quoted-printable:Nils Oliver Kr=C3=B6ger
n;quoted-printable:Kr=C3=B6ger;Nils Oliver
email;internet:[EMAIL PROTECTED]
version:2.1
end:vcard

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to