Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

sharing a complex set of objects between httpd processes

by thpfft (Chaplain)
on Mar 15, 2003 at 01:42 UTC ( [id://243231]=perlquestion: print w/replies, xml ) Need Help??

thpfft has asked for the wisdom of the Perl Monks concerning the following question:

I'll try and make this quick, because I know it's just a mixture of common questions but I've searched most diligently and can't find a definitive answer.

My application offers a singleton factory object as its front end: factory because if you pass it TT2 it gives the template author access to a wide range of classes, singleton because it's an expensive object and I don't want to have to make one for each request. It's initialised in a startup.pl, and thereafter every handler gets the same factory.

All this works fine within an httpd process: all requests make use of the same factory and therefore the same Config and Template and what have you. The next step is obvious: how can I get all of the httpd processes to share that single factory object and all its attendant machinery? They may have to share more than one object of this class, by the way: if two sites are running this application they'll have one factory each.

I know of various solutions that involve holding bits of data in a place that all the processes can reach - from Apache::Session hashes to dot files and databases - and others that use formal IPC and shared memory. It just seems like too much engineering: serialise this, synchronise that, tie up and flock the other. I've got a database for that sort of thing: I just need to share the means of getting at it.

In short, I want to create a single factory, give it to each httpd child as it starts up, and leave them all to get on with it. I suppose that means I need to require the relevant classes and create the factory at some very early stage of the startup process that happens before children are spawned. If anyone can either tell me how to do that or tell me where I'm being stupid, I'll be very grateful indeed.

Replies are listed 'Best First'.
Re: sharing a complex set of objects between httpd processes
by perrin (Chancellor) on Mar 15, 2003 at 02:01 UTC
    The short answer is, you can't do that. There is no way to share Perl objects between processes. There are things like IPC::Shareable that make it look like you can, but they all do the stuff you mentioned (serialization, locking, etc.) behind the scenes and just move objects around between processes.

    There are two pieces of good news though. The first is that there are ways of sharing data that are very fast, like IPC::MM and Cache::Mmap, despite all the machinery. The second is that you probably don't need to do this at all. If you create things before the fork in startup.pl, each child process will get a copy. If you need to share data, you have the above-mentioned methods and your RDBMS. If you need to coordinate access to some shared resource, you can do locking in various ways.

    If you explain why you think you need a singleton, we might be able to offer more specific advice about what to do.

      Thank you (both): very clear and helpful.

      The short answer is that I may well not need the singleton at all: it came from a quite possibly misguided idea of what would be an elegant and economical way to manage the proliferation of Class::DBI subclasses.

      I'm beginning to see that it's a relic of old bad cgi thinking. The fifty or so require()s involved in loading all the cdbi subclasses made the construction of the factory object seem much heavier than it really is, since all that presumably gets compiled away at the beginning. If I make sure the Template object and DBI handles are being managed sensibly, the difference made by the singleton is probably negligible.

      The one thing that I do need to share out among all the processes - apart from the database classes, which can look after themselves - is a new tied-DB-based inverted index. But that will actually get easier, I think, if I don't try and hang on to the factory object after a request has been processed.

      pardon me thinking out loud: i always find that making a fool of myself in public is the best way to lend urgency to the tired brain :(

        You are correct: require() and use() calls are basically free in mod_perl. Not sure what the implications are for the inverted index you mentioned. If it's in an RDBMS, you obviously have no problem. If it's in a dbm, you should consider using MLDBM::Sync for it.
•Re: sharing a complex set of objects between httpd processes
by merlyn (Sage) on Mar 15, 2003 at 01:53 UTC

      I'd also suggest Data::Serializer.


      cp

      "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: sharing a complex set of objects between httpd processes
by dws (Chancellor) on Mar 15, 2003 at 03:15 UTC
    The next step is obvious: how can I get all of the httpd processes to share that single factory object and all its attendant machinery?

    Move application logic into a single application server that the httpd processes make requests of. You an even pool connections to the application process.

      And will that application server be a single process? If not, he has the same problem all over again.
        This actually brings up an interesting issue - is there some clean and easy implementation that does this application server-type thing? I would suppose you can create a SOAP::Lite server or something, but it sounds like it would involve a lot of bandwidth ...

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://243231]
Approved by chromatic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-28 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found