Search code examples
pythonpyramid

Pyramid global object share across thread


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?


Solution

  • Any global variable is shared between threads in Python, so this part is really easy: "... create only one instance of that class ... before the server starts ... threads should be able to access it":

    corpus = MyCorpus()  # in global scope in any module
    

    Done! Then import the instance from anywhere and call your class' methods:

    from mydata import corpus
    
    corpus.do_stuff()
    

    No need for ZCA, plain pythonic Python :)

    (the general approach of keeping something large and very database-like within the webserver process feels quite suspicious though, I hope you know what you're doing. I mean - persistence? locking? sharing data between multiple processes? Redis, MongoDB and 1001 other database products have those problems solved)