调试以gevent模式运行的gunicorn程序
启动代码
在项目根目录下新建gapp.py,写入以下代码:
import multiprocessing
from gunicorn.app.base import BaseApplication
from app import create_app
class GunicornApplication(BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super().__init__()
def load_config(self):
config = {
key: value for key, value in self.options.items()
if key in self.cfg.settings and value is not None
}
for key, value in config.items():
self.cfg.set(key.lower(), value)
def load(self):
return self.application
app = create_app()
if __name__ == '__main__':
gunicorn_options = {
'bind': '0.0.0.0:5000',
'workers': (multiprocessing.cpu_count() * 2) + 1,
'worker_class': 'gevent'
}
GunicornApplication(app, gunicorn_options).run()
兼容协程配置
打开PyCharm设置窗口,选择Build, Execution, Deployment --> Python Debugger,勾选“兼容gevent”
启动调试
在gapp.py文件上点击右键,选择debug 'gapp’开始调试程序
调试celery程序
celery启动代码
celery_worker.py如下:
from app import create_app
from flask_celery import Celery
app = create_app()
celery = Celery(app)
if __name__ == '__main__':
celery.start()
运行配置
点击PyCharm中工具栏的Edit Configurations,新增名为celery的Configuration
启动调试
点击PyCharm右上角的debug按钮