0

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.

maxp
  • 5,454
  • 7
  • 28
  • 30
  • What kind of configuration are you using? Is your fastcgi server threaded or forked? – David Berger Mar 19 '09 at 22:38
  • Now I use threaded fastcgi and single object in module context but I'm not sure it is correct. I think it would not work for forked server. Do You know more common pythonic solution for that kind of problem? – maxp Mar 20 '09 at 13:53
  • I recommend a against using a singelton in this context. The application should work regardless of the threading model of the HTTP server - they should be decoupled. Can you explain what you need a singleton for? – Spike Gronim Oct 27 '10 at 23:08

1 Answers1

0

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!

Juergen
  • 12,378
  • 7
  • 39
  • 55