[Twisted-Python] flow example doesn't appear to be 'flowing'
Todd Greenwood
tgreenwoodgeer at yahoo.com
Tue Oct 4 18:08:14 MDT 2005
First, thanks for creating this cool tool!
I tried the following example (http://twistedmatrix.com/projects/flow/documentation/howto/flow.html
) ...and what I saw doesn't match the documentation. The docs state that if you launch two browsers
pointed at this url, that both pages will be built simultaneously, due to the flow/generator
constructs. However, when I tried this, I saw the web pages being generated sequentially. The second
page blocked until the first page finished. Is this simply my misunderstanding of the docs, or is
this page outdated?
REPRO:
$ twistd --version
twistd (the Twisted daemon) 2.0.1
Copyright (c) 2001-2004 Twisted Matrix Laboratories.
$ python flow-demo.py
visit http://localhost:8081/ to view the example
#mozilla browser tab #1 => http://localhost:8081
30 Random numbers:
1. 46905
2. 17955
3. 62281
4. 99248
5. 73635
#mozilla browser tab #2 => http://localhost:8081
blank until tab #1 has finished (has 30 random numbers)
DETAILS:
http://twistedmatrix.com/projects/flow/documentation/howto/flow.html
Flow Resources
By using flow.Deferred it is easy to make up a web resource which is both long running, but also can
serve more than one customer at a time. Run the example below, and with two browsers, view the
webpage. Notice that both web pages are being created at the same time.
from __future__ import generators
from twisted.internet import reactor
from twisted.web import server, resource
from twisted.flow import flow
def cooperative(count):
""" simulate a cooperative resource, that not block """
from random import random
idx = 0
while idx < count:
val = random()
yield flow.Cooperate(val)
yield str(val)[-5:]
idx += 1
def flowRender(req):
count = int(req.args.get("count",["30"])[0])
req.write("<html><body>")
req.write(" %s Random numbers: <ol>\n" % count)
source = flow.wrap(cooperative(count))
yield source
for itm in source:
req.write("<li>%s</li>\n" % itm)
yield source
req.write("</ol></body></html>\n")
class FlowResource(resource.Resource):
def __init__(self, gen):
resource.Resource.__init__(self)
self.gen = gen
def isLeaf(self): return true
def render(self, req):
self.d = flow.Deferred(self.gen(req))
self.d.addCallback(lambda _: req.finish())
return server.NOT_DONE_YET
print "visit http://localhost:8081/ to view the example"
root = FlowResource(flowRender)
site = server.Site(root)
reactor.listenTCP(8081,site)
reactor.run()
--
Todd Greenwood-Geer
tgreenwoodgeer at yahoo.com
________________________________________________________________________________
BEAWorld 2005: coming to a city near you. Everything you need for SOA and enterprise infrastructure success.
Register now at http://www.bea.com/4beaworld
London 11-12 Oct| Paris13-14 Oct| Prague18-19 Oct |Tokyo 25-26 Oct| Beijing 7-8 Dec
More information about the Twisted-Python
mailing list