I’m trying to write time counter with Twisted so that when I request GET /timer then it returns current count. First request starts the counter. Unfortunately, always when I request /timer it makes an additional new counter beginning from 0. Does anybody know, how to tweak the following code so that it doesn’t create new counter every GET /timer request)?
from twisted.internet import reactor
from twisted.web.resource import Resource
class TimeCounter(Resource):
def __init__(self):
self.value = 0
def test(self):
self.value += 1
print self.value # debug
reactor.callLater(1, self.test)
def render(self, request):
self.test()
return str(self.value)
resource = TimeCounter()
Thanks in advance.
Try this. I tested this out a few months ago and it worked fine. It will create a session cookie on the client.