使用 pycharm 创建一个 flask 项目;
将下面代码覆盖到项目中的 app.py;
from flask import Flask | |
from flask_apscheduler import APScheduler | |
import logging | |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s') | |
handler = logging.StreamHandler() | |
handler.setLevel(logging.DEBUG) | |
logger = logging.getLogger(__name__) | |
logger.addHandler(handler) | |
logger.handlers.extend(logging.getLogger("gunicorn.error").handlers) | |
class Config(object): | |
JOBS = [{ | |
'id': 'job1', | |
'func': 'app:job1', | |
'args': (1, 2), | |
'trigger': 'interval', | |
'seconds': 10 | |
}] | |
SCHEDULER_API_ENABLED = True | |
def job1(a, b): | |
logger.info(str(a) + ' ' + str(b)) | |
app = Flask(__name__) | |
app.config.from_object(Config()) | |
scheduler = APScheduler() | |
scheduler.init_app(app) | |
scheduler.start() | |
if __name__ == '__main__': | |
app.run() |
将项目压缩一下,放到 linux 服务器(因为 gunicorn 仅支持 unix 系统);
解压后,在项目路径下创建 logs 文件夹,并使用如下命令运行:
gunicorn --bind 0.0.0.0:8080 --workers 1 --access-logfile logs/access_log --error-log logs/error_log app:app
- 可以看到生成的 error_log 有日志输出
[2019-02-26 07:05:11 +0000] [10643] [INFO] Starting gunicorn 19.5.0 | |
[2019-02-26 07:05:11 +0000] [10643] [INFO] Listening at: http://0.0.0.0:8080 (10643) | |
[2019-02-26 07:05:11 +0000] [10643] [INFO] Using worker: sync | |
[2019-02-26 07:05:11 +0000] [10647] [INFO] Booting worker with pid: 10647 | |
[2019-02-26 07:05:21 +0000] [10647] [INFO] 1 2 | |
[2019-02-26 07:05:31 +0000] [10647] [INFO] 1 2 |