0

Usually, in an apache cgi-bin file, you response data this way:

print "Content-Type:text/html"
print 
print "Hello world"

And the browser shows the result once the script finishes. But what about closing the response (telling the client there's nothing else and close as if the script ended) so the web browser shows the content and python can keep doing other thing?

print "Content-Type:text/html"
print 
print urllib.urlopen("staticpage.html").read()
close() ?
do_a_backend_function # browser is not waiting and has showed the static page to the user

I suppose in the first case apache is taking care of that, but in the second I wanna force it. Thanks

Following @furas indications, I can guess I can just do:

print "Content-Type:text/html"
print 
print urllib.urlopen("staticpage.html").read()
import sys
sys.stdout.close()
do_a_backend_function() # browser is not waiting and has showed the static page to the user

Instead of using multithreading, which is much more difficult do develop. But I haven't tried it yet, hope someone does.

  • What do you mean, close the response? Do you mean closing the connection? Show us your code. – timgeb Jun 11 '14 at 06:11
  • @timgeb tell the client theres nothing else and can show the content in the browser –  Jun 11 '14 at 06:16
  • depends, are you using the socket module? showing us your code would really help. – timgeb Jun 11 '14 at 06:19
  • @timgeb not, just using print –  Jun 11 '14 at 06:21
  • http://stackoverflow.com/questions/1522636/should-i-call-close-after-urllib-urlopen maybe this helps – timgeb Jun 11 '14 at 06:24
  • You cannot do this in the same thread, you need to launch the `do_a_backend_function` in a separate process. – Burhan Khalid Jun 11 '14 at 07:33
  • @BurhanKhalid yeah, that's what I thought, maybe some help please... Or is it fine with just what furas said? –  Jun 11 '14 at 08:42

1 Answers1

0

print send text to sys.stdout so you could try to flush and close sys.stdout.

But I don't know what will happend next. Maybe Apache kill your process because sys.stdout is closed.

furas
  • 134,197
  • 12
  • 106
  • 148