Async http request in web2py

676 views
Skip to first unread message

Dane

unread,
Feb 17, 2011, 11:37:26 PM2/17/11
to web2py-users
Hi,

I need to make an asynchronous http request to one of my controller
functions in order to some lightweight background processing
(refreshing some cached objects). I came upon this thread:

http://groups.google.com/group/web2py/browse_thread/thread/bbe66f10843d6228/af4d1c0e8e8937d2?lnk=gst&q=web2py+async+http+request#af4d1c0e8e8937d2

But the link to the "lola" function posted post by Massimo is broken.
Can anyone supply a new link or explain a simple way to make async,
ajax-like requests from within web2py? Thanks.

-Dane

KMax

unread,
Feb 18, 2011, 8:52:59 AM2/18/11
to web2py-users
asynchronous http and synchronous http
What the difference ?
On 18 фев, 10:37, Dane <dane.schnei...@gmail.com> wrote:
> Hi,
>
> I need to make an asynchronous http request to one of my controller
> functions in order to some lightweight background processing
> (refreshing some cached objects). I came upon this thread:
>
> http://groups.google.com/group/web2py/browse_thread/thread/bbe66f1084...

Dane

unread,
Feb 18, 2011, 10:15:50 PM2/18/11
to web2py-users
Synchronous http makes the call and stops further execution until the
result is returned.

Async makes the call and optionally provides a callback so that
further code can be executed while waiting for the response. The way
ajax calls with in javascript, for example.

Seems like basic functionality--strange that something so useful isn't
built in to python in a straightforward way. It would be great if
web2py could do something to address this. There are many use cases
and it's much easier and lighter weight than dealing with threads,
processes, cron, etc. when all that is needed is a quick non-blocking
call.

Massimo Di Pierro

unread,
Feb 18, 2011, 11:31:04 PM2/18/11
to web2py-users
It is more complex than this. It is about two different models to
handle concurrency.

In one model you have one thread per request. Each request does IO
with the client within one thread. The web server creates and manages
the threads connects the thread to the web app (for example web2py)
via mod_wsgi.

In another model you do not have threads. You have async IO. It allows
to serve more clients at the same time if the requests are really
simple. If the computation is significative you may have concurrency
issues.

Then you can heve async io with ligthweight thread within is the best
of both worlds.

Python does not have built-in lightweight threads. web2py is based on
mod_wsgi which does not support async.

Anyway, this is not a definitive word on the subject.

Massimo

Dane

unread,
Feb 19, 2011, 12:19:00 AM2/19/11
to web2py-users
Thanks for the explanation Massimo. I figured it must be an issue with
python's architecture or something would have been added to the std
lib by now.

It's easy enough to simulate this behavior with ajax calls to
controllers from the client. Unfortunately since you can't rely on
javascript being enabled, it can't be used in every situation. Sort of
peculiar how a browser on the client's computer is more capable in
this area than a powerful web server.

It would be neat if someone set up a publish/subscribe api that could
act as an async proxy for synchronous servers. You would make a
request that included a target url and a callback url. Maybe something
like this already exists?


On Feb 18, 11:31 pm, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:

Vasile Ermicioi

unread,
Feb 19, 2011, 2:03:11 AM2/19/11
to web...@googlegroups.com
 Sort of
peculiar how a browser on the client's computer is more capable in
this area than a powerful web server.

If you need async calls you can take a look at gevent, eventlet or twisted

But put in your mind the main difference:
the browser is running continuously (until you close it), and this way ajax can work in background, 
but the request is ended at the moment you receive the response,
before returning the response you will need to wait for all async calls to be completed ( if you want them completed)

a good example of what I am talking is the new Datastore plus developed by Guido van Rossum for GAE http://code.google.com/p/appengine-ndb-experiment/
Reply all
Reply to author
Forward
0 new messages