473,503 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forking SocketServer daemon -- updating state

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 state that may take up to an hour to
build).

I would like to be able to notify the daemon that it needs to update its
state. Because it forks for each new request, a request handler can't
update the state because then only the child would have the new state.

One idea I had was to use signals. Is it safe to write a signal handler
that does extensive work (several seconds)? Seems like even so, it might
be tricky to do this without race conditions.

Another possibility is that the signal handler simply sets a needs_update
flag, which I could check for in a handle_request() loop. The disadvantage
here is that the update wouldn't happen until after the next request is
handled, and I would like the state to be available for that next request.
A solution might be to send a signal followed by a dummy request, which
seems a bit awkward.

Any other ideas or suggestions?

Thanks in advance,

Reid

p.s. This group's help on A* search was very much appreciated -- just the
ticket!
Feb 19 '07 #1
2 1831
Reid Priedhorsky wrote:
Another possibility is that the signal handler simply sets a needs_update
flag, which I could check for in a handle_request() loop. The disadvantage
here is that the update wouldn't happen until after the next request is
handled, and I would like the state to be available for that next request.
A solution might be to send a signal followed by a dummy request, which
seems a bit awkward.

Any other ideas or suggestions?
Just send a special type of request that signifies that an update is
wanted, and act on that?

Basically you need to find a way to let two processes talk to each
other. There are a lot of possibilities here (IPC). The simplest
would be to reuse the one you already have (the request handler!).
Another solution might be to use Pyro.
Or just open a custom socket yourself to send messages.
Or stick with your idea of using a signal handler. But I don't know
if you can let a signal handler run for a long time (as you already
asked yourself), and it won't be portable to Windows for instance.

You could maybe use threads instead, and then use some form of
thread synchronization primitives such as threading.Event
(threads would remove the need to do IPC).

Hope this helps a bit,

--Irmen
Feb 19 '07 #2
Reid Priedhorsky <re**@umn.eduwrote:
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 state that may take up to an hour to
build).

I would like to be able to notify the daemon that it needs to update its
state. Because it forks for each new request, a request handler can't
update the state because then only the child would have the new state.

One idea I had was to use signals. Is it safe to write a signal handler
that does extensive work (several seconds)? Seems like even so, it might
be tricky to do this without race conditions.
In general no it isn't safe.

However due to the way python handles its interrupts (it sets a flag
in its own signal handler which the bytecode interpreter examines and
acts upon when it is safe to do so) you won't corrupt python if you do
this.

I'd still recommend the set a flag and do it in your mainloop approach
though if you can.
Another possibility is that the signal handler simply sets a
needs_update flag, which I could check for in a handle_request()
loop. The disadvantage here is that the update wouldn't happen
until after the next request is handled, and I would like the state
to be available for that next request. A solution might be to send
a signal followed by a dummy request, which seems a bit awkward.
Can't you get SocketServer to timeout and return you control
regularly?

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Feb 20 '07 #3

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

Similar topics

3
4149
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
4397
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
2917
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...
12
13363
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
4454
by: Matt Stevens | last post by:
Hello, I'm having some trouble trying to get this simple forking http daemon to work, when I run it everything seems to run ok and then when I try to connect to it with a browser on another...
3
2079
by: czajnik | last post by:
Hi! I'm quite new to Python development. Can someone advise me a framework useful for building (pre-)forking or threaded TCP servers, other than SocketServer ? I've seen source code of a few...
10
2157
by: qwertycat | last post by:
I'm new to multi-process programming, should one avoid forking children from children of a parent? I'd like to spawn 10 children from the parent and each of those children spawns another 5...
0
1014
by: Reid Priedhorsky | last post by:
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...
3
5182
by: Scottman | last post by:
I am writing a server daemon that forks a child for every incoming request. The child process terminates when the client closes the connection. My problem is that I am unsure how to prevent the...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.