SCGIServer and unusal termination

Diez B. Roggisch deets at nospam.web.de
Tue Nov 17 10:59:40 EST 2009


Eden Kirin wrote:

> Hi there,
> 
> I'm playing with SCGIServer
> (http://vmlinux.org/cgi-bin/dwww/usr/share/doc/python-scgi/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 at 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 at 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 at sunce:~/data/project/ScgiServer/src> python test.py
> Starting server.
> ^CException!
> Terminating server, attempt 1.
> Check n: 2.
> eden at 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



More information about the Python-list mailing list