windows使用celery报错celery.exceptions.TimeoutError: The operation timed out.

今天研究这个也弄了比较久,最开始是没有往系统这个方向上去想。后来发现在windows上启动的方式和mac和linux这种不一样。

下面放上完整的启动方式和初步使用:

1、安装redis

redis的安装就不写了,最后只要能启动redis就行。ping一下看看有没有pong就知道是否启动成功。

2、安装eventlet

只有在windows系统上需要安装这个,用于启动

pip install eventlet
3、安装celery
下载依赖
pip install "celery[redis,auth,msgpack]"
启动celery
celery -A tasks(你自己的项目名) worker -P eventlet -l info
4、安装flower(用于监控)
下载依赖
pip install flower
启动服务
celery -A tasks flower
访问网址
http://localhost:5555/

5、初步使用

celery_proj/tasks.py:

from celery import Celery

# redis正确连接使用127.0.0.1,不能使用localhost
app = Celery("tasks", backend='redis://127.0.0.1:6379/', broker='redis://127.0.0.1:6379/')

@app.task
def add(x, y):
    return x + y

@app.task
def simple_task():
    return "Hello, World!"
celery_proj/run_tasks.py:

from tasks import add,simple_task

result = add.delay(4, 3)
print(result.get(timeout=1))
`

运行run_tasks:

python run_tasks.py

最终的结果:

因为运行的是add,最后结果应该为7

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误提示显示 Redis 需要进行身份验证,而 Celery Flower 没有提供身份验证信息。你需要在 Celery 的配置文件中添加 Redis 的身份验证信息。 具体步骤如下: 1. 打开 Celery 的配置文件,通常为 `celeryconfig.py` 或 `celery.py`。 2. 在配置文件中添加以下内容: ```python broker_url = 'redis://user:password@localhost:6379/0' ``` 其中,`user` 和 `password` 分别为 Redis 的用户名和密码,`localhost` 为 Redis 的地址,`6379` 为 Redis 的端口号,`0` 为 Redis 的数据库编号。 3. 保存配置文件并重新启动 Celery Flower。 如果你没有设置 Redis 的身份验证信息,可以在 Redis 中设置用户名和密码。具体步骤如下: 1. 打开 Redis 的配置文件,通常为 `redis.conf`。 2. 找到以下内容: ```conf # Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # # requirepass foobared ``` 3. 将 `requirepass` 的注释去掉,并将 `foobared` 替换为你要设置的密码。 4. 保存配置文件并重新启动 Redis。 然后,在 Celery 的配置文件中添加 Redis 的身份验证信息即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值