How can I share a blob of data using a Twisted server, while at the time time periodically updating that data in the background?:
from twisted.internet import reactor
from twisted.internet import task
from twisted.web.server import Site
from twisted.web.resource import Resource
data = 1
def update_data():
data += 1
class DataPage(Resource):
isLeaf = True
def render_GET(self, request):
return "<html><body>%s</body></html>" % (data, )
root = Resource()
root.putChild("data", DataPage())
factory = Site(root)
reactor.listenTCP(8880, factory)
m = task.LoopingCall(update_data)
m.start(10.0)
print "running"
reactor.run()
The above code does not work because of the following exception:
C:\temp>python discovery.py
Unhandled error in Deferred:
Traceback (most recent call last):
File "discovery.py", line 23, in <module>
m.start(10.0)
File "c:\python25\lib\site-packages\twisted\internet\task.py", line 163, in start
self()
File "c:\python25\lib\site-packages\twisted\internet\task.py", line 194, in __call__
d = defer.maybeDeferred(self.f, *self.a, **self.kw)
--- <exception caught here> ---
File "c:\python25\lib\site-packages\twisted\internet\defer.py", line 102, in maybeDeferred
result = f(*args, **kw)
File "discovery.py", line 10, in update_data
data += 1
exceptions.UnboundLocalError: local variable 'data' referenced before assignment
I would like HTTP clients to access, in this example, http://127.0.0.1:8880/data and retrieve the current value of data, while at the same time have some other task scheduled to update data every so often.
Moreover, I don't really want to use LoopingCall() because I may want to vary the interval depending on whether the update s开发者_运维技巧ucceeds or not; the update will be some sort of remote API call. Can I use CallLater() in some fashion instead?
I'm sure this is a stupid question! Thanks.
EDIT: thouis helped with properly making the data variable global. For those that follow, here's how to fit callLater()
into the code:
from twisted.internet import reactor
from twisted.internet import task
from twisted.web.server import Site
from twisted.web.resource import Resource
data = 1
def update_data():
global data
data += 1
reactor.callLater(10, update_data)
class DataPage(Resource):
isLeaf = True
def render_GET(self, request):
return "<html><body>%s</body></html>" % (data, )
root = Resource()
root.putChild("data", DataPage())
factory = Site(root)
reactor.listenTCP(8880, factory)
update_data()
print "running"
reactor.run()
This code still feels a bit hacky. I don't like declaring module-level variables, let alone using global variables. I welcome any suggestions that avoid such practises and make the code look cleaner and more re-usable.
Add a global def to update_data():
def update_data():
global data
data += 1
精彩评论