我正在尝试使用Apache 2.4部署Flask Web应用程序。该应用程序有一些后台线程,当使用内置服务器中的Flask(threaded = True参数)在开发环境中部署时,这些线程可以正常运行。
我阅读了有关如何使用Apache启用线程的信息,并修改了我的app.py文件。现在看起来像这样:
import threading
import atexit
import emailSender as em
from flask import Flask
POOL_TIME = 5 #Seconds
# variables that are accessible from anywhere
commonDataStruct = {}
# lock to control access to variable
dataLock = threading.Lock()
# thread handler
yourThread = threading.Thread()
def thread_function(name):
print ("Thread %s: starting", name)
time.sleep(2)
print ("Thread %s: finishing", name)
def create_app():
app = Flask(__name__)
@app.route('/hello')
def hello():
return "Hello World!"
def interrupt():
global yourThread
yourThread.cancel()
def doStuff():
print ("inside the thread method")
em.sendMail(msg, ["sourabh.iitg@gmail.com"])
def doStuffStart():
global yourThread
yourThread = threading.Timer(POOL_TIME, doStuff, ())
yourThread.start()
# Initiate
doStuffStart()
atexit.register(interrupt)
return app
我的web.wsgi文件已更新为
import sys
sys.path.insert(0, '<path>')
from app import create_app
application = create_app()
if __name__ == "__main__":
application.run()
现在,当我在Windows服务器上启动我的Apache服务时,我可以浏览路由,但是发送电子邮件的线程根本不会触发。我也没有在日志中看到任何错误。
是否需要其他配置来触发线程?