I want to update logfile as soon as a request comes. I have a class variable event_logging_enabled which is initialised to TRUE. and in the POST() function I check the value of event_logging_enabled.
Now at run time, I modify the value of this flag to FALSE for subsequent requests. But It remains TRUE.
During debugging, I found that when a request is received, a new object get created to handle each request, so, will pick initialized value i.e.TRUE.
This is not the case for other functions like getlogEnabled() of same class.
Can you please suggest any work around.
import web
import threading
class webServer(threading.Thread):
port = "1234"
event_logging_enabled = "True"
def getlogEnabled(self):
print "Stub getlogEnabled(): ",self.event_logging_enabled
def __init__(self):
threading.Thread.__init__(self)
""" Logging """
print "Init------------------------",self.event_logging_enabled
self.event_logging_filename = "ueLogs.log"
def run(self):
urls = (
'/','webServer',
)
app = web.application(urls,globals())
sys.argv.append(webServer.port)
app.run()
def POST(self):
print "in POST"
print "Stub POST(): Logging Enabled : ",self.event_logging_enabled
I’m not too familiar with web.py framework but in general with web applications, if you need to preserve the state across multiple requests you’ll have to manage it with a session object. The session object can be separate for each web user or common for the whole application.
There is a session object in the web.py framework:
http://webpy.org/docs/0.3/api#web.session
It lets you decide whether to store the content of the session in a database or directly in a file. The code sample under “DiskStore” on that page shows you how to place a variable in the session.
(By the way in Python boolean literals are True and False, not “True”).