在OpenShift Online上运行后台线程

时间:2017-04-11 19:38:00

标签: python python-3.x openshift mod-wsgi background-thread

我正在尝试调整使用继承自socketserver.TCPServer的处理程序的自定义http.server.SimpleHTTPRequestHandler以在OpenShift Online上运行。

原始代码运行一个非暂停的后台线程,该线程从第三方服务(异步到客户端处理)处理数据,并存储稍后可供客户端使用的输出。这个线程必须在服务器启动时启动一次,而不是再次(不是定期)启动,一旦启动它就不应该终止,除非整个应用程序已经终止。

OpenShift正在使用apache/mod_wsgi来运行应用程序,因此我以下列方式编写了wsgi.py

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()

这不起作用 - OpenShift不断重新导入文件并将thread_started标志重置为False,导致线程的多个实例同时运行。我怎样才能达到想要的行为?

0 个答案:

没有答案
相关问题