在PyCharm中调试celery、gunicorn

调试以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按钮

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值