How to make a singleton class with Python Flup fastcgi server?

1k views Asked by At

I use flup as fastcgi server for Django.

Please explain to me how can I use singleton? I'm not sure I understand threading models for Flup well.

1

There are 1 answers

0
Juergen On

If you use a forked server, you will not be able to have a singleton at all (at least no singleton that lifes longer than your actual context).

With a threaded server, it should be possibe (but I am not so much in Django and Web servers!).

Have you tried such a code (as an additional module):

# Singleton module
_my_singleton = None

def getSingleton():
   if _my_singleton == None:
      _my_singleton = ...
   return _my_singleton

At the tree dots ("...") you have to add coding to create your singleton object, of course.

This is no productive code yet, but you can use it to check if singletons will work at all with your framework. For singletons are only possible with some kind of "global storage" at hand. Forked servers make this more difficult.

In the case, that "normal global storage" does not work, there is a different possibility available. You could store your singleton on the file system, using Pythons serialization facilites. But of course, this would be much more overhead in deed!