471,075 Members | 762 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Safely updating master state in SocketServer with ForkingMixIn

Dear all,

I have a TCP server written using SocketServer with ForkingMixIn.
Servicing connections is CPU-bound and can take several seconds.

I now need a way to safely tell the master process to update its state (by
groveling in a PostgreSQL database, potentially for several seconds). How
can I do this safely?

I thought of two ways:

1. Install a signal handler for SIGHUP and update state when that signal
is received.

2. Stop using ForkingMixIn and fork myself, except when "special" requests
come in requesting a state update, which don't fork.

3. Look for the existence of a file after each request, and if it's there
update state.

Option 2 seemed like it would require duplicating a lot of code from
ForkingMixIn, and Option 3 seems kind of lame, so I started down Option 1.
I produced the following code snippets:

# main loop
while True:
global update_required
if (update_required):
# update state
update_required = False

# signal handler
def sighup(signum, frame):
global update_required
update_required = True

With this code, if the server is idle, SIGHUP causes it to immediately
drop out of handle_request() and execute the state updating block.

Do I have a race condition here? If the master process receives a SIGHUP
after a new connection starts but before a slave process is forked to
handle it, will that connection be messed up (to use highly technical
language)? Otherwise this technique seems compact and elegant.

Are there other options?

Your help would be much appreciated.


Jun 27 '08 #1
0 944

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Olivier Hoarau | last post: by
3 posts views Thread by Ergin Aytac | last post: by
reply views Thread by Adil Hasan | last post: by
5 posts views Thread by missiplicity | last post: by
12 posts views Thread by Paul Rubin | last post: by
1 post views Thread by rbt | last post: by
4 posts views Thread by DreJoh | last post: by
2 posts views Thread by Reid Priedhorsky | last post: by
reply views Thread by nisimura | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.