python - setting attribute in current_thread as state holder -
i have wsgi application can potentially run in different python web server environments(cherrypy, uwsgi or gunicorn), not greenlet-based. application handles different paths rest apis , user interfaces. during http call of app there need know, context of call, since implementation logic methods share code of api calls , ui calls , bunch of logic separated in many modules should react differently depending on context. simple , straightforward way pass parameter implementation code, e.g. apicall(caller=client_service_id) or usercall(caller=user_id), it's pain propagate parameter possible modules. solution set context in thread object this?
def set_context(ctx): threading.current_thread().ctx = ctx def get_context(): return threading.current_thread().ctx
so call set_context somewhere in beginning of http call handler can construct context ovject depending on environment data, , use get_context() in part of code must react depending on context. best practices achive this? thank you!
Comments
Post a Comment