Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   SCGIServer and unusal termination (http://www.velocityreviews.com/forums/t705462-scgiserver-and-unusal-termination.html)

Eden Kirin 11-16-2009 12:08 PM

SCGIServer and unusal termination
 
Hi there,

I'm playing with SCGIServer
(http://vmlinux.org/cgi-bin/dwww/usr/...gi/guide.html),
everything works just fine, but one thing bothers me. All prints after
try-except block are executed twice after the Ctrl+C is pressed!

test.py:
#-------------------------
from scgi.scgi_server import SCGIServer

n = 0
print "Starting server."

try:
SCGIServer().serve()
except (KeyboardInterrupt, SystemExit):
print "Exception!"

# print lines are executed twice (?!)
n += 1
print "Terminating server, attempt %d." % n
n += 1
print "Check n: %d." % n
#-------------------------

This is the output:

eden@sunce:~/data/project/ScgiServer/src> python test.py
Starting server.
^CException!
Exception!
Terminating server, attempt 1.
Check n: 2.
Terminating server, attempt 1.
Check n: 2.
eden@sunce:~/data/project/ScgiServer/src>


If I put something else in try-except block, code after is executed
normally:

try:
while 1:
pass
except (KeyboardInterrupt, SystemExit):
print "Exception!"

eden@sunce:~/data/project/ScgiServer/src> python test.py
Starting server.
^CException!
Terminating server, attempt 1.
Check n: 2.
eden@sunce:~/data/project/ScgiServer/src>

Environment is 64bit Ubuntu with Python v2.6.4.

Is there some reasonable explanation for this behaviour? Thanks in advance.

--
www.vikendi.net -/- www.supergrupa.com

Eden Kirin 11-17-2009 07:44 AM

Re: SCGIServer and unusal termination
 
Anyone?

--
www.vikendi.net -/- www.supergrupa.com

Diez B. Roggisch 11-17-2009 03:59 PM

Re: SCGIServer and unusal termination
 
Eden Kirin wrote:

> Hi there,
>
> I'm playing with SCGIServer
> (http://vmlinux.org/cgi-bin/dwww/usr/...gi/guide.html),
> everything works just fine, but one thing bothers me. All prints after
> try-except block are executed twice after the Ctrl+C is pressed!
>
> test.py:
> #-------------------------
> from scgi.scgi_server import SCGIServer
>
> n = 0
> print "Starting server."
>
> try:
> SCGIServer().serve()
> except (KeyboardInterrupt, SystemExit):
> print "Exception!"
>
> # print lines are executed twice (?!)
> n += 1
> print "Terminating server, attempt %d." % n
> n += 1
> print "Check n: %d." % n
> #-------------------------
>
> This is the output:
>
> eden@sunce:~/data/project/ScgiServer/src> python test.py
> Starting server.
> ^CException!
> Exception!
> Terminating server, attempt 1.
> Check n: 2.
> Terminating server, attempt 1.
> Check n: 2.
> eden@sunce:~/data/project/ScgiServer/src>
>
>
> If I put something else in try-except block, code after is executed
> normally:
>
> try:
> while 1:
> pass
> except (KeyboardInterrupt, SystemExit):
> print "Exception!"
>
> eden@sunce:~/data/project/ScgiServer/src> python test.py
> Starting server.
> ^CException!
> Terminating server, attempt 1.
> Check n: 2.
> eden@sunce:~/data/project/ScgiServer/src>
>
> Environment is 64bit Ubuntu with Python v2.6.4.
>
> Is there some reasonable explanation for this behaviour? Thanks in
> advance.


I can only guess that SCGIServer does something to stdout. Your code isn't
executed twice, so the doubling seems to come from writing it twice.

Try e.g. redirecting stdout and stderr to different files, and see if things
appear once in both.

Diez

Eden Kirin 11-17-2009 04:32 PM

Re: SCGIServer and unusal termination
 
Diez B. Roggisch wrote:

>> Is there some reasonable explanation for this behaviour? Thanks in
>> advance.

>
> I can only guess that SCGIServer does something to stdout. Your code isn't
> executed twice, so the doubling seems to come from writing it twice.


Yes I know that code isn't executed twice since the value of n remains
the same, only print lines are doubled.

> Try e.g. redirecting stdout and stderr to different files, and see if things
> appear once in both.


Redirection of stdout:

eden@sunce:~/data/project/ScgiServer/test> python test.py 1> output.txt
^Ceden@sunce:~/data/project/ScgiServer/test> cat output.txt
Starting server.
Exception!
Terminating server, attempt 1.
Check n: 2.
Starting server.
Exception!
Terminating server, attempt 1.
Check n: 2.

Redirecting stderr creates an empty file. I still haven't found the
solution.

--
www.vikendi.net -/- www.supergrupa.com

Diez B. Roggisch 11-17-2009 05:30 PM

Re: SCGIServer and unusal termination
 
Eden Kirin wrote:

> Diez B. Roggisch wrote:
>
>>> Is there some reasonable explanation for this behaviour? Thanks in
>>> advance.

>>
>> I can only guess that SCGIServer does something to stdout. Your code
>> isn't executed twice, so the doubling seems to come from writing it
>> twice.

>
> Yes I know that code isn't executed twice since the value of n remains
> the same, only print lines are doubled.
>
>> Try e.g. redirecting stdout and stderr to different files, and see if
>> things appear once in both.

>
> Redirection of stdout:
>
> eden@sunce:~/data/project/ScgiServer/test> python test.py 1> output.txt
> ^Ceden@sunce:~/data/project/ScgiServer/test> cat output.txt
> Starting server.
> Exception!
> Terminating server, attempt 1.
> Check n: 2.
> Starting server.
> Exception!
> Terminating server, attempt 1.
> Check n: 2.
>
> Redirecting stderr creates an empty file. I still haven't found the
> solution.
>


Then

- save a reference to sys.stdout *before* invoking the server
- compare to it after interruption. If it has changed, you at least know
that somebody messed with it, and can beat him or whatever you see fit.

Diez

Eden Kirin 11-19-2009 09:32 AM

Re: SCGIServer and unusal termination
 
Diez B. Roggisch wrote:

> - save a reference to sys.stdout *before* invoking the server
> - compare to it after interruption. If it has changed, you at least know
> that somebody messed with it, and can beat him or whatever you see fit.


Thanks for the help. Finally, I dropped python-scgi module and I wrote
my own SCGI server. There was not only the problem with stdout, but one
much serious which made it unusable to me. Every SCGI request was forked
as a new process, without the ability to access the shared globals
within the project.

--
www.vikendi.net -/- www.supergrupa.com

Дамјан Георгиевски 11-19-2009 03:45 PM

Re: SCGIServer and unusal termination
 
> everything works just fine, but one thing bothers me. All prints after
> try-except block are executed twice after the Ctrl+C is pressed!
>
> test.py:
> #-------------------------
> from scgi.scgi_server import SCGIServer
>
> n = 0
> print "Starting server."
>
> try:
> SCGIServer().serve()
> except (KeyboardInterrupt, SystemExit):
> print "Exception!"
>
> # print lines are executed twice (?!)
> n += 1
> print "Terminating server, attempt %d." % n
> n += 1
> print "Check n: %d." % n
> #-------------------------


SCGIServer().serve() forks, so it seems that there are 2 python
processes continuing to run after SCGIServer().serve()


--
дамјан ((( http://damjan.softver.org.mk/ )))

Spammers scratch here with a diamond to find my address:
|||||||||||||||||||||||||||||||||||||||||||||||

Eden Kirin 11-19-2009 05:03 PM

Re: SCGIServer and unusal termination
 
Дамјан Георгиевски wrote:

> SCGIServer().serve() forks, so it seems that there are 2 python
> processes continuing to run after SCGIServer().serve()


I noticed that which makes it unusable to me. Also, it took me almost
whole day to realize this. I'm adopting a huge application to work with
SCGI which shares a certain amount of data between working threads and
SCGI handler. I couldn't realize the cause of erratic and unconsistent
data behaviour. After diving into python-scgi code, I gave it up and
wrote my own SCGI server.

--
www.vikendi.net -/- www.supergrupa.com


All times are GMT. The time now is 12:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.