[Twisted-Python] why flow?
Clark C. Evans
cce at clarkevans.com
Sat Apr 19 02:28:28 EDT 2003
# Why flow?
# The flow module provides a mechanism for delivering incremental
# results and performing long computations in a manner which
# cooperates with other simoutaneous requests.
# In this example are two data sources, one of which simulates
# a long-computation which is made to cooperate with other
# requests, and the other which blocks, perhaps simulating a
# database result that delivers content in small chunks as it
# pages through the query results.
# Using these two data sources is a simple HTML web page,
# which weaves the data together onto a single output
# stream, via request.write
# Why not Deferreds?
# One could use deferreds, however, in the first, cooperative
# example, one would really have to use a thread or break the
# computation down into very small deferred chunks (where one
# really isn't necessary with the flow module). And, in the
# second, either you'd need a deferred for each row (or chunk
# of rows), or the results could not be incrementally delivered.
# And, further, the HTML page generation would have to be broken
# up into several 'stages' which were executed after each
# other. Furthermore, if more than one 'database' query
# was to be used, the deferreds from them would have to
# be synconized; or, you could not execute the queries
# in parallel (one on a different database node, for
# example). In any case, I don't think it'd be as
# pretty as this... or am I missing something?
from __future__ import generators
from twisted.internet import reactor
from twisted.web import server, resource
""" simulate a cooperative resource, that doesn't block """
from random import random
val = random()
""" simulate a blocking database resource """
from time import sleep
yield (2,'DC','District of Columbia')
yield (6,'NH','New Hampshire')
yield (7,'NJ','New Jersey')
yield (8,'NM','New Mexico')
yield (9,'NY','New York')
yield (0,'NC','North Carolina')
yield (1,'ND','North Dakota')
yield (9,'RI','Rhode Island')
yield (0,'SC','South Carolina')
yield (1,'SD','South Dakota')
yield (9,'WV','West Virginia')
zips = flow.wrap(cooperative())
states = flow.wrap(flow.ThreadedIterator(blocking()))
for cnt, abbr, state in states:
""" % (abbr, state))
for x in range(cnt):
if x: req.write(", ")
def isLeaf(self): return true
def render(self, req):
root = FlowResource()
site = server.Site(root)
More information about the Twisted-Python