python - Use Twisted's getPage as urlopen? -


i use twisted non-blocking getpage method within webapp, feels quite complicated use such function compared urlopen.

this example of i'm trying achive:

def web_request(request):    response = urllib.urlopen('http://www.example.org')    return httpresponse(len(response.read())) 

is hard have similar getpage?

the thing realize non-blocking operations (which seem explicitly want) can't write sequential code them. operations don't block because don't wait result. start operation , return control function. so, getpage doesn't return file-like object can read urllib.urlopen does. , if did, couldn't read until data available (or block.) , can't call len() on it, since needs read data first (which block.)

the way deal non-blocking operations in twisted through deferreds, objects managing callbacks. getpage returns deferred, means "you result later". can't result until it, add callbacks deferred, , deferred call these callbacks when result is available. callback can want to:

def web_request(request)     def callback(data):         httpresponse(len(data))     d = getpage("http://www.example.org")     d.addcallback(callback)     return d 

an additional problem example web_request function blocking. want while wait result of getpage become available? else within web_request, or wait? or want turn web_request non-blocking? if so, how want produce result? (the obvious choice in twisted return deferred -- or same 1 getpage returns, in example above. may not appropriate if you're writing code in framework, though.)

there is way write sequential code using deferreds, although it's restrictive, harder debug, , core twisted people cry when use it: twisted.internet.defer.inlinecallbacks. uses new generator feature in python 2.5 can send data generator, , code this:

@defer.inlinecallbacks def web_request(request)     data = yield getpage("http://www.example.org")     httpresponse(len(data)) 

like example explicitly returned d deferred, this'll work if caller expects web_request non-blocking -- defer.inlinecallbacks decorator turns generator function returns deferred.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -