Hi,
I am trying to spawn a daemon type program to go off on its own and do
some work (asynchoronousl y) from within an HTTPServer, but I am running into
a problem where the web browser (actually a Perl LWP program) still seems to
be waiting for more input until the child that is forked in the server
finishes (granchild, actually). I don't understand this. I need to be able
to have the POST to the server complete and go on even though there is a
(grand)child process still running.
So... that's the short summary, more specific code follows..
worker.py is a program that does the standard double-fork paradigm,
allowing the original parent to return and leaving an orphaned child behind
to do its own thing. If you call this program directly through the shell, it
returns you immediately to your shell, which is just like what I want and
would expect:
#! /usr/bin/python
"""
worker.py
"""
import sys, os, time
t = 30
pid = os.fork()
if (pid == 0):
# child: fork() again and exit so that the original
# parent is not left waiting on us.
pid = os.fork()
if (pid > 0):
# child
print "child (PID %d): forked grandchild %d. I'm exiting." %\
(os.getpid(), pid)
sys.exit()
# grandchild continues here, drops out bottom of if-block
elif (pid > 0):
# parent: exit so caller can continue
p_pid = os.getpid()
print "parent (PID %d): forked child %d. ." % (p_pid, pid)
(c_pid, status) = os.wait()
print "parent (PID %d): child %d is done. I'm exiting." % (p_pid, c_pid)
sys.exit() # normal exit
# grandchild continues here - my worker process
g_pid = os.getpid()
s = "grandchild (PID %d): going to sleep for %d seconds..." % (g_pid, t)
print s
# pretend to do some work
time.sleep(t)
# done
print "grandchild (PID %d) waking up... exiting." % g_pid
Here is sample output from running worker.py:
ej@sand:~/src/python/problem> worker.py
grandchild (PID 9697): going to sleep for 30 seconds...
child (PID 9696): forked grandchild 9697. I'm exiting.
parent (PID 9695): forked child 9696. .
parent (PID 9695): child 9696 is done. I'm exiting.
ej@sand:~/src/python/problem>
and then about 30 sec later, after getting back to my shell, my console
prints:
grandchild (PID 9697) waking up... exiting.
This is all fine and dandy. One step removed from that is to call this
program from another Python program via os.system(). It too exits
immediately and returns me to the shell even though my (grand)child process
is still running. Again, this is just what I want and expect. Here is the
code for that program:
#! /usr/bin/python
"""
call_worker.py
"""
import os, sys
PROG_NAME = sys.argv[0]
print PROG_NAME + ": calling worker.py..."
os.system('work er.py')
print PROG_NAME + ": all done. exiting."
Here is the output from running it (again, all fine and dandy):
ej@sand:~/src/python/problem> call_worker.py
../call_worker.py: calling worker.py...
grandchild (PID 9710): going to sleep for 30 seconds...
child (PID 9709): forked grandchild 9710. I'm exiting.
parent (PID 9708): forked child 9709. .
parent (PID 9708): child 9709 is done. I'm exiting.
../call_worker.py: all done. exiting.
ej@sand:~/src/python/problem>
and after 30 seconds, your again-active shell window should print:
grandchild (PID 9710) waking up... exiting.
But if I make this same os.system() call from within my own HTTPServer,
then the browser that is making the request is left hanging until that
sleeping grandchild is done. It's like there is still a socket connection
between it and my browser?!? But the grandchild is not forked from the
server - it's an os.system() call! It should not be inherting any file
descriptors and such, right? Even though the do_GET() function should be
over, and in fact, you can see in the shell that started the server that the
server process has already exited, the browser is apparently still waiting
for the web page to finish loading.
I don't get it! I have a long task I need to instantiate in my Python
server based on info in the HTTP POST, and the program that is making that
POST needs to be able to go on and do other stuff before my task is over.
Why am I not getting a clean exit in my POSTing process? What do I need to
do to get my HTTP request to my server to finish essentially "immediatel y"?
Thanks for taking the time to read my post. Any help greatly appreciated!
:) -ej
Below is the code for a simple server you can run. It is currently coded
to listen on port 5238 and only serve one request before the server exits.
It serves a little static chunk of HTML on a GET request regardless of what
PATH you call (I talked about POST above, but the browser-hanging behaviour
is the same).
#! /usr/bin/python
"""
server.py
"""
from BaseHTTPServer import HTTPServer, BaseHTTPRequest Handler
import os, sys
SERVER_PORT = 5238 # arbitrary port not in use (open on firewall?)
WORKER = 'worker.py' # name of the program to call via os.system()
PROG = sys.argv[0] # name of this program
#============== =============== =============== =============== ============
class RequestHandler( BaseHTTPRequest Handler):
#-------------------------------------------------------------------
def do_GET(self):
# call my "daemon" to get some stuff done in the background
print "%s: about to call os.system(%s)" % (PROG, WORKER)
os.system(WORKE R)
print "%s: back from os.system(%s)" % (PROG, WORKER)
# send HTTP headers
self.send_respo nse(200)
self.send_heade r("Content-type", 'text/html');
self.end_header s()
# start HTML output
html = """\
<html>
<body><h2>All done!</h2></body>
</html>
"""
self.wfile.writ e(html)
#-------------------------------------------------------------------
#============== =============== =============== =============== ============
# BEGIN main
if (__name__ == '__main__'):
# create a new HTTP server and handle a request
server = HTTPServer(('', SERVER_PORT), RequestHandler)
server.handle_r equest() 1 2702
In article <41********@nnt p.zianet.com>,
"Erik Johnson" <ej <at> wellkeeper <dot> com> wrote:
.... I'm not sure why those would all be open, but this little bit seems to resolve the problem (it throws OSError when it hits the first invalid file descriptor).
for fd in xrange(3, 256): try: os.close(fd) except OSError: break
If you know a better/smarter way to effect the same thing, I'd be glad to hear about it.
That's a variation on a common idiom. The difference is
the break, where more commonly you'd write "pass". If this
is happening in the context of a short-term programming effort
that has to be very efficient, it's a good idea, because the
exceptions make the close much more expensive than it would
have been in C, where we usually see this loop. If it needs
to work for the indefinite future, I would say change the break
to pass. You can't count on that continuous series of open
file descriptors.
Donn Cave, do**@u.washingt on.edu This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: WmGill |
last post by:
I'm new to Python, and am dabbling with it to replace a lot of scattered
scripts & programs.
One thing I want to try is an HTTPserver like in the pydocgui module. This
way I can use html to create a user interface to an interactive script (i.e.
for access to a MySQL db). Unfortunately, I get lost trying to follow a
2100 line script. Best I can figure tk provides the thread & loop to keep
things running, but I'm not familiar enough...
|
by: Will Stuyvesant |
last post by:
I have a CGI server written in Python, useful for offline CGI testing.
One of my CGI scripts ends with:
print 'Location: http://myresourcelocation'
print
But this does not work with the Python CGI server...and the same
script *does* work via internet with the Apache server at my work.
Does the BaseHTTPServer.HTTPServer not support the HTTP Location
|
by: aswinee |
last post by:
I am running Microsoft SQL Server 2000 - 8.00.760 Enterprise Edition
on Windows 2003 Enterprise Edition (NT 5.2 Build 3790:) I have 4CPU
and 8GB of RAM. I have AWE enabled, /pae /3gb switch is on in
boot.ini, In my errorlog I have noticed few times error messages as
"The SQL Server cannot obtain a LOCK resource at this time. Rerun your
statement when there are fewer active users or ask the system
administrator to check the SQL Server lock...
|
by: Chris Travers |
last post by:
Hi All;
I may be able to do this in Perl, but if there is enough interest in doing
something like this in C, maybe I can still help (however, consider your
self warned about my skill at coding C).
I am looking at the following design. The clustering daemon either need to
run on separate computer systems or separate interfaces of the same database
servers. The program would basically pretend to be Postmaster and intercept
the...
|
by: Michael Per |
last post by:
Does anybody know of a best way to limit system resources (CPU/memory) for
each particular request? In my application based on user's parameters a
request may take considerable amount of time and system resources. This kind
of request may take 100% CPU and make all other requests in the queue halt.
While I don't want this request to simply time out, I'd like to limit
somehow the resources each request can allocate so one request does not...
| |
by: crowell |
last post by:
Hello,
I am having trouble getting the ThreadingMixIn to do what I want.
Looking over the docs (and some old code I wrote which successfully
does what I want), I arrived at the following:
import time
import SocketServer
import BaseHTTPServer
|
by: djc |
last post by:
I get this *intermittently* on a utility I am working on. I don't know whats
going on but here are a few points about it:
- using VS 2005, running on xp sp2
- program uses multiple threadpool threads
- only happens when built with the 'release' configuration (no debug flag,
compiler optimizations in effect) - no problems in debug config
here is the error:
Unhandled Exception: System.AccessViolationException: Attempted to read or
write...
|
by: troywalker |
last post by:
I am new to LDAP and Directory Services, and I have a project that
requires me to authenticate users against a Sun Java System Directory
Server in order to access the application. I have found dozens of
examples of how to authenticate users against Active Directory, but AD
seems to be a different animal than Sun Java System Directory Server.
Could someone provide me with an example of how to authenticate a user
against a Directory...
|
by: =?Utf-8?B?cmVk?= |
last post by:
Hi Friends,
We recently deployed our application to production and I am experiencing the
below error message.
Cannot access a disposed object named "System.Net.TlsStream"
The error occurs when we are making a web service call to an external system
(within our corporate n/w). The method is POST and the URL is over HTTPS.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |