Is it possible to set a thread or something alike to store a value among each server request?
I understand that values from threads or globals are get/set only during each server request, also this values cannot be changed out of context because everything is done inside "mapped" classes.
Eventhough I am just being curious out of this because it would be great to set a value and manipulate it over each request, I don't know it may be a variable stored in memory so that it may be referenced later.
I am saying this because sessions, cookies and everthing related to a web application is manipulated only through each GET/POST request. o far this is what I have:
import web
import threading
threadlocal = threading.local()
def set_value():
setattr(threadlocal, 'mythread', 'threadvalue')
def get_value():
return getattr(threadlocal, 'mythread', None)
urls = (
"/set_thread_value", "set_thread_value",
"/get_thread_value", "get_thread_value"
)
app = web.application(urls, globals())
t_globals = {
'template_thread': get_value()
}
render = web.template.render('templates', globals=t_globals)
class set_thread_value:
def GET(self):
set_value()
return get_value()
class get_thread_value:
def GET(self):
return get_value()
As you can see when you call "set_thread_value" you set the thread value and it is displayed on screen, but when you call "get_thread_value" you will get None instead.
You may also have noticed that I want to pass the thread to a template so that it can be displayed with its updated value.
Edit:I don't want to use mysql, mongodb or any other storing system to set the value from the thread.
Thanks in advance.