Phyton Pushing data once a URL is requested python passing data between threads,python passing data between classes,

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

How would I get the second request to trigger this from the initial response? I'm planning on possibly creating a fancy [engines that support multipart/x-mixed-replace (currently only Gecko)]-only email webapp that does server-push and Ajax effects without any JavaScript, just for fun.

Answer:1

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.

A recent development is evserver, a lightweight WSGI server, which implements the Asynchronous WSGI extension proposed by Christopher Stawarz in the Web-SIG in May 2008.

Finally, the Tornado web server supports non-blocking asynchronous requests. It has a chat example application using long polling, which has similarities with your requirements.

Answer:2

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.

Answer:3

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.

Answer:4



  1. python passing data between threads
  2. python passing data between classes
  3. python passing data between functions
  4. python passing data between processes
  5. python passing data
  6. python passing data between modules
  7. python passing data to thread
  8. python passing data between scripts

I have a Docker file trying to deploy Django code to a container FROM ubuntu:latest MAINTAINER { myname } #RUN echo "deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -sc) main universe" >> /...

I have a Docker file trying to deploy Django code to a container FROM ubuntu:latest MAINTAINER { myname } #RUN echo "deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -sc) main universe" >> /...

I've been playing with the community edition of JetBrains ItelliJ IDEA since it came out, and I really like it; however, a lot of plugins seem to require the ultimate edition (or at least I think they ...

I've been playing with the community edition of JetBrains ItelliJ IDEA since it came out, and I really like it; however, a lot of plugins seem to require the ultimate edition (or at least I think they ...