in reply to How do I make a PSGI program do costly initialisation only once per process, not per thread?

If your server really is using threads, just set up a shared variable maybe? You will still have to handle race conditions and thundering herds during the initialization.

If your server is forking, you're out of luck and you'll need some out-of-process way of caching things.

Update

The $env will tell you what your server does:

psgi.multithread: This is a boolean value, which MUST be true if the application may be simultaneously invoked by another thread in the same process, false otherwise.

psgi.multiprocess: This is a boolean value, which MUST be true if an equivalent application object may be simultaneously invoked by another process, false otherwise.

I didn't find any hooks for "run this on process startup" or "run this code on thread startup".

Update2: If you are really using threads, I would stare long and intently on the documentation of the database library on whether it needs reinitialization per-thread or if it does the initialization transparently. I wouldn't expect database handles to naturally work as shared resources across threads.

  • Comment on Re: How do I make a PSGI program do costly initialisation only once per process, not per thread?
  • Download Code