spawnl and waitpid

N

naima.mans

hello,

I run a python cgi script under Apache...

and while the script is running i want to display a "please wait"
message until the script finish.

I have tried to do this but the "please wait" message appears at the
script end (which is useless at this time! )

here my script:
------------------------------------------------------------------------------------------------
def get_ret(status):
signal = status & 0xff
if signal == 0:
retcode = status >> 8
else:
retcode = 0
return signal,retcode

pidActuel = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py")

if ( get_ret(os.waitpid(pidActuel,0))[1] == 0 ):
print """ END"""
else:
print """ please wait... """

-------------------------------------------------------------------------------------------------
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hello,

I run a python cgi script under Apache...

and while the script is running i want to display a "please wait"
message until the script finish.

I have tried to do this but the "please wait" message appears at
the script end (which is useless at this time! )
You should flush sys.stdout after you print messages, or
the message will keep in buffer untill buffer is full or process
terminated.


- --
Thinker Li - (e-mail address removed) (e-mail address removed)
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF5Afi1LDUVnWfY8gRAgaoAJ9fccAjo00QupE7SRFqgbmOUGZMugCgjvdH
cFoxm+jiZiIpKOfd+fHCt/M=
=9COv
-----END PGP SIGNATURE-----
 
N

naima.mans

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1






You should flush sys.stdout after you print messages, or
the message will keep in buffer untill buffer is full or process
terminated.

- --
Thinker Li - (e-mail address removed) (e-mail address removed)://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org

iD8DBQFF5Afi1LDUVnWfY8gRAgaoAJ9fccAjo00QupE7SRFqgbmOUGZMugCgjvdH
cFoxm+jiZiIpKOfd+fHCt/M=
=9COv
-----END PGP SIGNATURE-----

Hello

thanks for answering
i have flush like this but same result .. the server wait until the
end...

pid = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py")
sys.stdout.flush()
ret = os.waitpid(pid,0)

if ( ret[1] != 0):
print """ wait %i """ %ret[1]
sys.stdout.flush()
else:
print """ end %i """ %ret[1]
sys.stdout.flush()
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You should flush sys.stdout after you print messages, or the
message will keep in buffer untill buffer is full or process
terminated.


thanks for answering i have flush like this but same result .. the
server wait until the end...
pid = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py") sys.stdout.flush() ret =
os.waitpid(pid,0)
Your program will be blocked here until child process being terminated.
You should print your messages before this line.
if ( ret[1] != 0): print """ wait %i """ %ret[1]
sys.stdout.flush() else: print """ end %i """ %ret[1]
sys.stdout.flush()




- --
Thinker Li - (e-mail address removed) (e-mail address removed)
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF5BWS1LDUVnWfY8gRAkp8AKCAcTKi/MO6sfkGBBEcMjfpH42O1wCeN14I
0AZ83oVacK0hKik4YC/jfCA=
=3h7d
-----END PGP SIGNATURE-----
 
N

naima.mans

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1





You should flush sys.stdout after you print messages, or the
message will keep in buffer untill buffer is full or process
terminated.
Hello
thanks for answering i have flush like this but same result .. the
server wait until the end...
pid = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py") sys.stdout.flush() ret =
os.waitpid(pid,0)

Your program will be blocked here until child process being terminated.
You should print your messages before this line.
if ( ret[1] != 0): print """ wait %i """ %ret[1]
sys.stdout.flush() else: print """ end %i """ %ret[1]
sys.stdout.flush()

- --
Thinker Li - (e-mail address removed) (e-mail address removed)://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org

iD8DBQFF5BWS1LDUVnWfY8gRAkp8AKCAcTKi/MO6sfkGBBEcMjfpH42O1wCeN14I
0AZ83oVacK0hKik4YC/jfCA=
=3h7d
-----END PGP SIGNATURE------ Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

thanks again

i have tried as you said (cf bellow) and same result...
is there any link which explain how a server Web read script and send
the answer ?

-------------------------------------------------------------------------
pid = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py")
ret = os.waitpid(pid,0)
print """please wait ...."""
sys.stdout.flush()
# retourne le process id
if ( ret[1] != 0):
print """ wait %i """ %ret[1]
sys.stdout.flush()
else:
print """ end %i """ %ret[1]
sys.stdout.flush()
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

i have tried as you said (cf bellow) and same result... is there
any link which explain how a server Web read script and send the
answer ?

print 'please wait...'
ret = os.waitpid(pid,0)

You have missed my idea.
Since os.waitpid will be blocked, you should print your message before
calling os.waitpid().


- --
Thinker Li - (e-mail address removed) (e-mail address removed)
http://heaven.branda.to/~thinker/GinGin_CGI.py

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF5HA61LDUVnWfY8gRAu8sAJ4n1dogsw7RzTxH8Ke3xnNX6gXnRQCeMOKf
/dsGHttcJc/KGpx414I7rxw=
=E3o7
-----END PGP SIGNATURE-----
 
N

naima.mans

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



print 'please wait...'


You have missed my idea.
Since os.waitpid will be blocked, you should print your message before
calling os.waitpid().

- --
Thinker Li - (e-mail address removed) (e-mail address removed)://heaven.branda.to/~thinker/GinGin_CGI.py

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org

iD8DBQFF5HA61LDUVnWfY8gRAu8sAJ4n1dogsw7RzTxH8Ke3xnNX6gXnRQCeMOKf
/dsGHttcJc/KGpx414I7rxw=
=E3o7
-----END PGP SIGNATURE-----

hello

ha ok...

i tried this one and the browser don't write the message at once... I
have alos tried with thread and have the same result... :(

----------------------------------------------
pid = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py")
print 'please wait...'
sys.stdout.flush()
ret = os.waitpid(pid,0)
# retourne le process id
if ( ret[1] != 0):
print """ wait %i """ %ret[1]
sys.stdout.flush()
else:
print """ end %i """ %ret[1]
sys.stdout.flush()
-----------------------------

--------- WITH THREAD ----------------

class AsyncLaunch(threading.Thread):
def __init__(self, infile):
threading.Thread.__init__(self)
self.infile = infile
def run(self):
os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python",self.infile)
print 'Fin background'

print """please wait..."""
sys.stdout.flush()
sys.stdout.close()
###### # print "<a href=\\\\Fr9033256d\execute\%s>fichier de log </
a>" %config.nom_log
background = AsyncLaunch('Main.py')
background.start()
######
print """The main program continues to run in foreground. wait..."""
print """ <a href='http://fr9033256d/cgi-bin/SARAT/go.py'>clic</a>"""
background.join() # Wait for background task to finish
print """Main program waited until background was done."""
 
D

Dennis Lee Bieber

i have tried as you said (cf bellow) and same result...

No, you did NOT do as was suggested (unless you misunderstood which
line "before this" meant.
is there any link which explain how a server Web read script and send
the answer ?

As soon as you execute os.waitpid(), your script WAITS. NOTHING
after that point will happen until the process you are waiting on exits.
print """please wait ...."""
sys.stdout.flush()
# retourne le process id
if ( ret[1] != 0):
print """ wait %i """ %ret[1]
sys.stdout.flush()
else:
print """ end %i """ %ret[1]
sys.stdout.flush()



pid = os.spawnl(os.P_NOWAIT,
"c:\\python25\\python.exe",
"python",
"Main.py") #apparently a Windows OS
print "Please wait ..."
sys.stdout.flush()
ret = os.waitpid(pid, 0)

if ret[1]:
print "Non-zero exit code: %s %s" % (ret[1], ret[0])
else:
print "Successful exit"
sys.stdout.flush()

{I'm presuming a 0 return code means success... VMS used 1 for success
and 0 for failure [actually, odd numbers were informational status, even
numbers were error status]}


--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

i tried this one and the browser don't write the message at once...
I have alos tried with thread and have the same result... :(

Does child-process terminate it-self in a very short time ?
It can be a race condition, here.
You can try to sleep for a while in child-process to make sure parent
being executed. Parent process may not schedule to run immediately
after spawn. And, you can print some thing before os.spawnl() to make
sure that message can be delivered successfully.


- --
Thinker Li - (e-mail address removed) (e-mail address removed)
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF5Hkp1LDUVnWfY8gRAtDdAKCKy8/ap5VJvZV14nhSCWYfLZdyYACffJ+Y
0sHMgyaQBmsOMwq/rxEvm1Q=
=qjTU
-----END PGP SIGNATURE-----
 
N

naima.mans

i have tried as you said (cf bellow) and same result...

No, you did NOT do as was suggested (unless you misunderstood which
line "before this" meant.
is there any link which explain how a server Web read script and send
the answer ?

As soon as you execute os.waitpid(), your script WAITS. NOTHING
after that point will happen until the process you are waiting on exits.
print """please wait ...."""
sys.stdout.flush()
# retourne le process id
if ( ret[1] != 0):
print """ wait %i """ %ret[1]
sys.stdout.flush()
else:
print """ end %i """ %ret[1]
sys.stdout.flush()

pid = os.spawnl(os.P_NOWAIT,
"c:\\python25\\python.exe",
"python",
"Main.py") #apparently a Windows OS
print "Please wait ..."
sys.stdout.flush()
ret = os.waitpid(pid, 0)

if ret[1]:
print "Non-zero exit code: %s %s" % (ret[1], ret[0])
else:
print "Successful exit"
sys.stdout.flush()

{I'm presuming a 0 return code means success... VMS used 1 for success
and 0 for failure [actually, odd numbers were informational status, even
numbers were error status]}

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/

hello

thanks a lot..

sorry for misunderstanding, my english isn't good (i'm french).

i have tried like this:
----------------------------------------------
#! C:/Python25/python.exe
# -*- coding: cp1252 -*-


import cgitb; cgitb.enable()
import os,sys

print "Content-Type: text/html"
print

pid = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py")
print "please wait.."
sys.stdout.flush()
ret = os.waitpid(pid,0)
# retourne le process id
if ret[1]:
print "Non-zero exit code: %s %s" % (ret[1], ret[0])
else:
print "Successful exit"
sys.stdout.flush()
--------------------------------------------------------

and the result:

At the end of the script execution it write:
==>>>> please wait.. Successful exit

The browser doesn't let the script executing on background to write
the message at once.. it waits the end.


what happend?
 
N

naima.mans

On 27 Feb 2007 05:39:51 -0800, (e-mail address removed) declaimed the
following in comp.lang.python:
No, you did NOT do as was suggested (unless you misunderstood which
line "before this" meant.
As soon as you execute os.waitpid(), your script WAITS. NOTHING
after that point will happen until the process you are waiting on exits.
print """please wait ...."""
sys.stdout.flush()
# retourne le process id
if ( ret[1] != 0):
print """ wait %i """ %ret[1]
sys.stdout.flush()
else:
print """ end %i """ %ret[1]
sys.stdout.flush()
pid = os.spawnl(os.P_NOWAIT,
"c:\\python25\\python.exe",
"python",
"Main.py") #apparently a Windows OS
print "Please wait ..."
sys.stdout.flush()
ret = os.waitpid(pid, 0)
if ret[1]:
print "Non-zero exit code: %s %s" % (ret[1], ret[0])
else:
print "Successful exit"
sys.stdout.flush()
{I'm presuming a 0 return code means success... VMS used 1 for success
and 0 for failure [actually, odd numbers were informational status, even
numbers were error status]}
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/

hello

thanks a lot..

sorry for misunderstanding, my english isn't good (i'm french).

i have tried like this:
----------------------------------------------
#! C:/Python25/python.exe
# -*- coding: cp1252 -*-

import cgitb; cgitb.enable()
import os,sys

print "Content-Type: text/html"
print

pid = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py")
print "please wait.."
sys.stdout.flush()
ret = os.waitpid(pid,0)
# retourne le process id
if ret[1]:
print "Non-zero exit code: %s %s" % (ret[1], ret[0])
else:
print "Successful exit"
sys.stdout.flush()
--------------------------------------------------------

and the result:

At the end of the script execution it write:
==>>>> please wait.. Successful exit

The browser doesn't let the script executing on background to write
the message at once.. it waits the end.

what happend?- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

re hello

perhaps there is a link with the script being called..
as there is a function which use stdout:
here it is
--------------------------------------------
def erreur_ccm(cmd):
try:
child_stdin, child_stdout, child_stderr =os.popen3(cmd)
stderr=child_stderr.read()
stdout=child_stdout.read()
if stderr == "":
retour = stdout
else :
retour = 0
except OSError, e:
child_stdin.close()
child_stdout.close()
child_stderr.close()

return retour
----------------------------------------------
 
N

naima.mans

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



Does child-process terminate it-self in a very short time ?
It can be a race condition, here.
You can try to sleep for a while in child-process to make sure parent
being executed. Parent process may not schedule to run immediately
after spawn. And, you can print some thing before os.spawnl() to make
sure that message can be delivered successfully.

- --
Thinker Li - (e-mail address removed) (e-mail address removed)://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org

iD8DBQFF5Hkp1LDUVnWfY8gRAtDdAKCKy8/ap5VJvZV14nhSCWYfLZdyYACffJ+Y
0sHMgyaQBmsOMwq/rxEvm1Q=
=qjTU
-----END PGP SIGNATURE-----

hello

oki, i have test a simple script that only wait...
when i tun the script without the server it write on 2 time which is
normal:
================================================
Content-Type: text/html
wait
The main program continues to run in foreground.
[[[[(((then after 5 second ))]]]]]]]
Fin background
Main program waited until background was done.
==================================================

BUT when i call it under the server web it write after 5 second all
the message in 1 time :
===================================================
wait The main program continues to run in foreground. Fin background
Main program waited until background was done.
===================================================

-----------------------THE SCRIPT ---------------------------
import cgitb; cgitb.enable()
import os,sys
import threading, zipfile,time,Main

print "Content-Type: text/html"
print
print "wait"
sys.stdout.flush()

class AsyncZip(threading.Thread):
def __init__(self, infile):
threading.Thread.__init__(self)
self.infile = infile
def run(self):
time.sleep(5.0)
print 'Fin background'

background = AsyncZip('Main.py')
background.start()
print 'The main program continues to run in foreground.'
sys.stdout.flush()
background.join() # Wait for background task to finish
print 'Main program waited until background was done.'
sys.stdout.flush()
---------------------------------------------------------------
 
N

naima.mans

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Does child-process terminate it-self in a very short time ?
It can be a race condition, here.
You can try to sleep for a while in child-process to make sure parent
being executed. Parent process may not schedule to run immediately
after spawn. And, you can print some thing before os.spawnl() to make
sure that message can be delivered successfully.
- --
Thinker Li - (e-mail address removed) (e-mail address removed)://heaven.branda..to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org
iD8DBQFF5Hkp1LDUVnWfY8gRAtDdAKCKy8/ap5VJvZV14nhSCWYfLZdyYACffJ+Y
0sHMgyaQBmsOMwq/rxEvm1Q=
=qjTU
-----END PGP SIGNATURE-----

hello

oki, i have test a simple script that only wait...
when i tun the script without the server it write on 2 time which is
normal:
================================================
Content-Type: text/html
wait
The main program continues to run in foreground.
[[[[(((then after 5 second ))]]]]]]]
Fin background
Main program waited until background was done.
==================================================

BUT when i call it under the server web it write after 5 second all
the message in 1 time :
===================================================
wait The main program continues to run in foreground. Fin background
Main program waited until background was done.
===================================================

-----------------------THE SCRIPT ---------------------------
import cgitb; cgitb.enable()
import os,sys
import threading, zipfile,time,Main

print "Content-Type: text/html"
print
print "wait"
sys.stdout.flush()

class AsyncZip(threading.Thread):
    def __init__(self, infile):
        threading.Thread.__init__(self)
        self.infile = infile
    def run(self):
        time.sleep(5.0)
        print 'Fin background'

background = AsyncZip('Main.py')
background.start()
print 'The main program continues to run in foreground.'
sys.stdout.flush()
background.join()    # Wait for background task to finish
print 'Main program waited until background was done.'
sys.stdout.flush()
---------------------------------------------------------------- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

hello

The pb seems to be solve but no idee why as i have done lot of
changes :/ (may be the last flush or the "\n".. i'm gone
investigate..)

THANKSSSSSSSSSSSSSSSSS for helping :)

for information here the code

----------------------------------------------------------

#! C:/Python25/python.exe -u
# -*- coding: cp1252 -*-
import cgitb; cgitb.enable()
import os, commands, time,Parser,config
import cgi

import sys,Function
sys.stderr = sys.stdout
print "Content-Type: text/html"
print

pid = os.spawnl(os.P_NOWAIT,"c:\\python25\
\python.exe","python","Main.py")
print "please wait...\n"
print """Le fichier de log est consultable sur \\\\fr9033256d
\execute\%s \n""" %config.nom_log
sys.stdout.flush()
ret = os.waitpid(pid,0)
if ret[1]:
print "Non-zero exit code: %s %s" % (ret[1], ret[0])
else:
print "Successful exit"
sys.stdout.flush()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top