Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sharing class instance among threads

I have a class, that loads all resources into memory that are needed for my application (mostly images).

Then several threads need to access these resources through this class. I don't want every instance to reload all resources, so I thought I use the Singleton Pattern. I did it like this:

class DataContainer(object):
    _instance = None
    _lock = threading.Lock()
    _initialised = True

    def __new__(cls, *args, **kwargs):
        with cls._lock:
            if not cls._instance:
                cls._initialised = False
                cls._instance = object.__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self, map_name = None):

        # instance has already been created
        if self._initialised:
            return

        self._initialised = True

        # load images

This works fine, as long as I am not using multiple threads. But with multiple Threads every thread has a different instance. So using 4 threads, they each create a new instance. I want all threads to use the same instance of this class, so the resources are only loaded into memory once.

I also tried to do this in the same module where the class is defined, but outside the class definition:

def getDataContainer():
    global dataContainer
    return dataContainer

dataContainer = DataContainer()

but every thread still has its own instance.

I am new to python, if this is the wrong approach plz let me know, I appreciate any help

like image 262
user2078645 Avatar asked Feb 23 '14 20:02

user2078645


People also ask

Are class variables shared between threads in Python?

As mentioned earlier, each thread shares the same memory space. That is, the variables in the program are shared by all the threads and cannot be accessed the way you would normally access a variable.

Are class variables shared between threads?

Yes they are shared, so you have to handle exclusion with some primitives or if you can afford it use "synchronized" methods. Threads don't share anything by themselves, you have to make them share whatever it is fields/properties.

How can we share data between the threads?

All static and controlled data is shared between threads. All other data can also be shared through arguments/parameters and through based references, as long as the data is allocated and is not freed until all of the threads have finished using the data.


1 Answers

To expand on @Will's comment, if a "shared object" is created by the parent, then passed in to each thread, all threads will share the same object.

(With processes, see the multiprocessing.Manager class, which directly support sharing state, including with modifications.)

import threading, time


class SharedObj(object):
    image = 'beer.jpg'


class DoWork(threading.Thread):
    def __init__(self, shared, *args, **kwargs):
        super(DoWork,self).__init__(*args, **kwargs)
        self.shared = shared

    def run(self):
        print threading.current_thread(), 'start'
        time.sleep(1)
        print 'shared', self.shared.image, id(self.shared)
        print threading.current_thread(), 'done'


myshared = SharedObj()
threads = [ DoWork(shared=myshared, name='a'), 
            DoWork(shared=myshared, name='b')
]
for t in threads:
    t.start()
for t in threads:
    t.join()
print 'DONE'

Output:

<DoWork(a, started 140381090318080)> start
<DoWork(b, started 140381006067456)> start
shared beer.jpg shared140381110335440
 <DoWork(b, started 140381006067456)> done
beer.jpg 140381110335440
<DoWork(a, started 140381090318080)> done
DONE

Note that the thread IDs are different, but they both use the same SharedObj instance, at memory address ending in 440.

like image 91
johntellsall Avatar answered Oct 13 '22 23:10

johntellsall