List Info

Thread: WSGI and asyncronous applications support




WSGI and asyncronous applications support
user name
2007-09-18 06:39:23
Hi.

I have read some old posts about asynchronous applications
support in WSGI:

http://comments.gmane.org/gmane.com
p.python.twisted.web/2561?set_lines=100000
http://comments.gmane.org/gmane.comp
.python.twisted.web/632?set_lines=100000


Since nginx *does* not supports threads, it is important to
add 
extensions to WSGI to support asynchronous applications.


There are two solutions:
1) The applications returns something like
environ['wsgi_not_yet_done']
    and produce its output using the write callable, and the
finish
    callable when its done
2) The applications calls resume =
environ['wsgi.pause_output'](), yield
    an empty strings, and then calls resume() to notify the
server that
    it can call .next() again



1) should be very simple to implement, and it is easy to
understand how 
to use it.

As an example, we can use some API that calls a callback
function when 
the result is available:
conn.execute("SELECT * FROM test",
query_callback)

def query_callback(row):
    write(row[...])


2) Can be implemented in mod_wsgi, however my problem is
that I can't 
figure out how the application can yield some data available
after a 
callback is called.



Thanks  Manlio Perillo
_______________________________________________
Web-SIG mailing list
Web-SIGpython.org
Web SIG: http://www.python.
org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/bo
nd%40yahoo.com

Re: WSGI and asyncronous applications support
country flaguser name
United States
2007-09-18 07:40:57
On Tue, 18 Sep 2007 13:39:23 +0200, Manlio Perillo
<manlio_perillolibero.it> wrote:
> [snip]
>
>1) should be very simple to implement, and it is easy to
understand how
>to use it.
>
>As an example, we can use some API that calls a callback
function when
>the result is available:
>conn.execute("SELECT * FROM test",
query_callback)
>
>def query_callback(row):
>    write(row[...])
>
>
>2) Can be implemented in mod_wsgi, however my problem is
that I can't
>figure out how the application can yield some data
available after a
>callback is called.
>

I think you figured it out already, actually:

    def app(...):
        conn.execute("SELECT * FROM test",
query_callback)
        # indicate not-done-yet, however

    def query_callback(...):
        write(...)
        conn.execute("SELECT * FROM test2",
another_callback)

    def another_callback(...):
        write(...)
        finish()

If you can have one callback, then there's no reason you
shouldn't be
able to have an arbitrary number of callbacks.

Of course, this could also be expressed in a less error
prone manner:

    def app(...):
        test_deferred = conn.execute("SELECT * FROM
test")
        test_deferred.addCallback(query_callback)
        return test_deferred

    def query_callback(...):
        write(...)
        test2_deferred = conn.execute("SELECT * FROM
test2")
        test2_deferred.addCallback(another_callback)
        return test2_deferred

    def another_callback(...):
        write(...)

  ;)

Jean-Paul
_______________________________________________
Web-SIG mailing list
Web-SIGpython.org
Web SIG: http://www.python.
org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/bo
nd%40yahoo.com

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )