473,287 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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:
server.handle_request()
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.

Thanks,

Reid
Jun 27 '08 #1
0 1009

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Olivier Hoarau | last post by:
Hello, I have build a client/server application with the socket module. The server mades UDP broadcasting and the client only reads UDP broadcast messages. All work fine. Now I want to use for...
3
by: Ergin Aytac | last post by:
I'm trying to run a script written in python and have some socket connection problems. I cutted the origin script (more than 1000 lines) so it is only the part of the connection and there is no...
0
by: Adil Hasan | last post by:
Hello Fred, I just ran across your question. I think that the following code will work: ----- SERVER CODE ------ import SocketServer import time class...
5
by: missiplicity | last post by:
Hi, I am newbie to Python language and am taking my baby steps. I am using Python2.4 from ActiveState on W2K. I am trying to create a simple SocketServer program. Just adding the following 2 lines...
12
by: Paul Rubin | last post by:
Let's say you have a SocketServer with the threading mix-in and you run serve_forever on it. How can you shut it down, or rather, how can it even shut itself down? Even if you use a...
1
by: rbt | last post by:
I've read more about sockets and now, I have a better understanding of them. However, I still have a few SocketServer module questions: When used with SocketServer how exactly does...
4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
2
by: Reid Priedhorsky | last post by:
Hi folks, I am implementing a forking SocketServer daemon that maintains significant internal state (a graph that takes ~30s to build by fetching from a SQL database, and eventually further...
0
by: nisimura | last post by:
Hi, I noticed that when I used SocketServer.ForkingMixIn (ForkingTCPServer), there were always zombie processes around. I searched for where waitpid() is called in ForkingMixIn and found it in...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.