I am trying to adapt a custom socketserver.TCPServer
which uses a handler inheriting from http.server.SimpleHTTPRequestHandler
to run on OpenShift Online.
The original code runs a non-halting background thread which crunches data from third party service (asynchronous to client handling) and stores output to be later available for clients. It is necessary that this thread launches exactly once when the server is started and never again (not periodically), and once it is started it should not terminate unless entire application has been terminated.
OpenShift is using apache/mod_wsgi
to run applications, so I wrote the wsgi.py
in the following manner:
thread_started = False
def application(environ, start_response):
.... # client handling logic here
def my_thread():
global thread_started
if thread_started:
return
thread_started = True
.... # background thread logic here
threading.Thread(target=my_thread).start()
This doesn't work - OpenShift constantly reimports the file and resets my thread_started
flag to False
, causing multiple instances of the thread running simultaneously. How can I achieve wanted behaviour?