Given, when a user requests
/foo on my server, I send the following HTTP response (not closing the connection):
Content-Type: multipart/x-mixed-replace; boundary=----------------------- ----------------------- Content-Type: text/html <a target='_blank' href="/bar">foo</a>
When the user goes to
/bar (which will send
204 No Content so the view doesn't change), I want to send the following data in the initial response.
----------------------- Content-Type: text/html bar
No complete answer, but:
In your question, you're describing a Comet-style architecture. Regarding support of Comet-style techniques in Python/WSGI, there is a StackOverflow question, which talks about various Python servers with support for long-running requests a la Comet.
Also interesting is this mail thread in the Python Web-SIG: "Could WSGI handle Asynchronous response?". In May 2008, there was a broad discussion in the Web-SIG about the topic of asynchronous requests in WSGI.
If the problem is to pass some command from /bar application to /foo application and you are using some servlet-like approach (the Python code is loaded once and not for each request as in CGI), you can just change some class property of the /foo application and be ready to react to the change in the /foo instance (by checking the property state).
Obviously the /foo application should not return right after the first request and yield content line by line.
Thought this is just theory, I have not tried that myself.
I have created some small example (just for fun, you know :))
import threading num = 0 cond = threading.Condition() def app(environ, start_response): global num cond.acquire() num += 1 cond.notifyAll() cond.release() start_response("200 OK", [("Content-Type", "multipart/x-mixed-replace; boundary=xxx")]) while True: n = num s = "--xxx\r\nContent-Type: text/html\r\n\r\n%s\n" % n yield s # wait for num change: cond.acquire() while num == n: cond.wait() cond.release() from cherrypy.wsgiserver import CherryPyWSGIServer server = CherryPyWSGIServer(("0.0.0.0", 3000), app) try: server.start() except KeyboardInterrupt: server.stop() # Now whenever you visit http://127.0.0.1:3000/, the number increases. # It also automatically increases in all previously opened windows/tabs.
The idea of a shared variable and thread synchronization (using condition variable object) is based on the fact that WSGI server provided by CherryPyWSGIServer is threaded.