Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid global object share across thread

Tags:

python

pyramid

I have a small pyramid web service.

I have also a python class that creates an index of items and methods to search fast across them. Something like:

class MyCorpus(object):

    def __init__(self):
        self.table = AwesomeDataStructure()

    def insert(self):
        self.table.push_back(1)

    def find(self, needle):
        return self.table.find(needle)

I would like to expose the above class to my api.

I can create only one instance of that class (memory limit).

So I need to be able to instantiate this class before the server starts. And my threads should be able to access it.

I also need some locking mechanism(conccurrent inserts are not supported).

What is the best way to achieve that?

like image 663
gosom Avatar asked Feb 07 '23 17:02

gosom


1 Answers

Add an instance of your class to the global application registry during your Pyramid application's configuration:

config.registry.mycorpus = MyCorpus()

and later, for example in your view code, access it through a request:

request.registry.mycorpus

You could also register it as a utility with Zope Component Architecture using registry.registerUtility, but you'd need to define what interface MyCorpus provides etc., which is a good thing in the long run. Either way having a singleton instance as part of the registry makes testing your application easier; just create a configuration with a mock corpus.

Any locking should be handled by the instance itself:

from threading import Lock

class MyCorpus(object):

    def __init__(self, Lock=Lock):
        self.table = AwesomeDataStructure()
        self.lock = Lock()

    ...

    def insert(self):
        with self.lock:
            self.table.push_back(1)
like image 88
Ilja Everilä Avatar answered Feb 11 '23 16:02

Ilja Everilä