2
我有一個簡單的瓶的應用程序,這樣說:無法配置Gunicorn服務燒瓶中的應用程序運行的另一個迴路同時
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
我也有鬆弛的機器人閱讀郵件
#bot.py
def serve(self):
while True:
message, channel = self.parse_slack_output(self.slack_client.rtm_read())
if message and channel:
self.handle_message(message, channel)
time.sleep(self.READ_WEBSOCKET_DELAY)
我希望兩個代碼同時運行。因此,在app.py
我做的:
#app.py
if __name__ == "__main__":
import threading
import bot
flask_process = threading.Thread(target=app.run)
bot_process = threading.Thread(target=bot.serve)
bot_thread.start()
flask_thread.start()
此代碼與$ python app.py
預期,但是當我在gunicorn
帶來機器人線程似乎並沒有工作。
我曾嘗試:
gunicorn app:app
gunicorn --workers=2 app:app
gunicorn --threads=2 app:app
我也試過multiprocessing
庫,並得到了相同的結果。
任何想法如何解決這個問題?謝謝。
編輯:我現在明白這個問題有多糟糕。我不應該在if __name__ = "__main__":
塊中編寫代碼。這不是由gunicorn
運行的。它直接拿起應用程序並運行它。還必須弄清楚如何使它處理機器人線程。