1

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?

Mirac7
  • 1,566
  • 4
  • 26
  • 44
  • You could have multiple instances (processes) with your application as the configuration for Apache/mod_wsgi used by OpenShift could set it up that way. Also, depending on what configuration is, the processes could be restarted. It is bad practice to be doing what you are doing from a web application process. You should be using a separate task queueing system or service. – Graham Dumpleton Apr 13 '17 at 13:30

0 Answers0