CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Net::Server::Prefork help

 



alager
Novice

Sep 1, 2009, 10:25 AM

Post #1 of 6 (1066 views)
Net::Server::Prefork help Can't Post

I have two questions:

1)I'm looking for some help on making each child maintain it's own connection to a mysql database using DBI. I've tried putting the DB connection in post_bind_hook(), post_configure_hook() and pre_loop_hook(). But when I check the fullprocesslist, I only see one connection from my application, even though I have 5 children preforked. I don't think that process_connection() would be a good place, because I would like the DB connection to be maintained, not set up and destroyed with each incoming connection.

2)I want to create a hash table that each child has access to, but I need to be able to update the table without restarting the script, so that each child has access to the same information. I think that having the parent maintain the hash, and the children read from the parent via a socket or pipe or semaphore, would work, but I'm not sure how/where to place the code for the parent, to do this.

Thanks for your help,
Aaron


1arryb
User

Sep 3, 2009, 7:48 AM

Post #2 of 6 (1039 views)
Re: [alager] Net::Server::Prefork help [In reply to] Can't Post

Hi alager,

Looking at the manual, It's not clear to me that any of the hooks that you tried happen after the server has forked. Maybe a better place for the DBI connection would be in the child_init_hook()? To eliminate some of the mystery, try replacing the DBI connection with something that justs prints the process id or server id. If all the ids are the same, you know you're using the wrong hook.

Cheers,

Larry


alager
Novice

Sep 3, 2009, 9:56 AM

Post #3 of 6 (1036 views)
Re: [1arryb] Net::Server::Prefork help [In reply to] Can't Post

Awesome! Thanks Larry, that works! Smile
There are still a few hooks that need to be documented I guess.
I noticed that the child_init_hook() didn't have a stub, so I though it might be defined somewhere else.

So looking into item 2, would using IPC::Sharable at the top of my script and creating a shared hash, before I invoke Net::Server, be a good method? Then while in process_request() I should be able to read from the hash in the child processes. Then in the $self->loop() I can add a function call to maintain the hash itself.

Any thoughts on this?

Thanks,
Aaron


1arryb
User

Sep 3, 2009, 10:16 AM

Post #4 of 6 (1034 views)
Re: [alager] Net::Server::Prefork help [In reply to] Can't Post

alager,


Quote
There are still a few hooks that need to be documented I guess.

Net::Server::PreFork is a subclass of Net::Server::PreForkSimple, which is where child_init_hook() is defined. Read about it in the superclass' perldoc.

You're already using 2 powerful IPC mechanisms here; why add a 3rd? Either: a) Use a db table for the "hash" through the existing DBI handles; or b) Have your driver script instantiate a second Net::Server to serve the hash to the Net::Servers handling the db connections?

Cheers,

Larry


alager
Novice

Sep 3, 2009, 10:57 AM

Post #5 of 6 (1032 views)
Re: [1arryb] Net::Server::Prefork help [In reply to] Can't Post

I'm actually doing option "a" right now. But the thought is that in the "real world" we're going to have several thousand devices contacting the Net::Server, every 15-30 seconds. Part of the Servers task is to check the devices ID, then insert the data from a specific device into a specific table in the DB. So if I can reduce the DB hit (the DB is located on a different machine) and turn it into a local RAM access, that would reduce the load in a significant way and increase the number of clients devices that can be handled.

Option B sounds interesting, but I'm not able to wrap my head around it yet...I'm not sure how in the same script I could have two Net::Servers when all the hooks will have the same names, eg. process_request().

Thanks for your help,
Aaron


1arryb
User

Sep 3, 2009, 12:22 PM

Post #6 of 6 (1029 views)
Re: [alager] Net::Server::Prefork help [In reply to] Can't Post

Hi alager,

Thinking more about it, there's probably no point in a separate server for id service. It's basically the same thing as 1 server with 2 service methods.

For id service implementation you have (at least) 2 ways to go:

1) Use a lightweight, local multi-user db (e.g., sqlite, mysql) just for your ids and maintain a second DBI handle to it in your db servers. This should be pretty fast (faster than the updates to your remote db, that's for sure). Using an external db has the additional benefit that it keeps your DBServer simple. You don't have to implement service methods to get/set device ids. If you tune the db right, you can usually get it to cache most of the data in RAM.

2) The Perl heap solution. Here, you will have to bight the bullet and implement some sort of pipe or socket communication between the parent and children of your DB server. You say you want to update the id hash w/o restarting the servers, too. That means a locking/update interface which makes me even more motivated to use a well-tested multiuser db.

Others can probably add additional solutions I haven't thought of.

Good luck,

Larry


(This post was edited by 1arryb on Sep 3, 2009, 12:25 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives