python解决任务问题_python使用celery做异步任务时的常见的问题

我这里用的是celery redis的组合,rabbitmq有些重型。

启动的时候报错,查了下文档,问题确认是全局变量一个参数的问题。

[root@devops-ruifengyun ~ ]$

[root@devops-ruifengyun ~ ]$ celery -A tasks worker --loglevel=debug

Running a worker with superuser privileges when the

worker accepts messages serialized with pickle is a very bad idea!

If you really want to continue then you have to set the C_FORCE_ROOT

environment variable (but please think about this before you do).

User information: uid=0 euid=0 gid=0 egid=0

[root@devops-ruifengyun ~ ]$ export C_FORCE_ROOT="true"

[root@devops-ruifengyun ~ ]$ celery -A tasks worker --loglevel=debug

/usr/local/lib/python2.7/dist-packages/celery/platforms.py:762: RuntimeWarning: You are running the worker with superuser privileges, which is

absolutely not recommended!

Please specify a different user using the -u option.

解决办法:

export C_FORCE_ROOT="true"

293

–> 294 meta = self._get_task_meta_for(task_id)

295 if cache and meta.get(‘status’) == states.SUCCESS:

296 self._cache[task_id] = meta

AttributeError: ‘DisabledBackend’ object has no attribute ‘_get_task_meta_for’

pip install celery-with-redis

测试的代码:

[root@devops-ruifengyun ~ ]$ cat celeryconfig.py

BROKER_URL = "redis://127.0.0.1:6379/0"

CELERY_IMPORTS = ("tasks.add", )

[root@devops-ruifengyun ~ ]$

[root@devops-ruifengyun ~ ]$ cat test.py

from tasks import add

if __name__ == '__main__':

for i in range(100):

for j in range(100):

kk=add.delay(i, j)

kk.ready()

kk.get()

The full contents of the message body was:

{‘utc’: True, ‘chord’: None, ‘args’: (0, 0), ‘retries’: 0, ‘expires’: None, ‘task’: ‘tasks.add’, ‘callbacks’: None, ‘errbacks’: None, ‘timelimit’: (None, None), ‘taskset’: None, ‘kwargs’: {}, ‘eta’: None, ‘id’: ’48c13a71-ecda-4ef8-98ce-f223a28c0b97′} (209b)

Traceback (most recent call last):

File “/usr/local/lib/python2.7/dist-packages/celery/worker/consumer.py”, line 455, in on_task_received

strategies[name](message, body,

KeyError: ‘tasks.add’

[2014-04-23 09:57:16,673: ERROR/MainProcess] Received unregistered task of type ‘tasks.add’.

The message has been ignored and discarded.

Did you remember to import the module containing this task?

Or maybe you are using relative imports?

Please see http://bit.ly/gLye1c for more information.

The full contents of the message body was:

{‘utc’: True, ‘chord’: None, ‘args’: (0, 0), ‘retries’: 0, ‘expires’: None, ‘task’: ‘tasks.add’, ‘callbacks’: None, ‘errbacks’: None, ‘timelimit’: (None, None), ‘taskset’: None, ‘kwargs’: {}, ‘eta’: None, ‘id’: ‘eb1b8772-ca7f-4966-8033-7b4db2a82aa6’} (209b)

Traceback (most recent call last):

File “/usr/local/lib/python2.7/dist-packages/celery/worker/consumer.py”, line 455, in on_task_received

strategies[name](message, body,

KeyError: ‘tasks.add’

解决的办法:

最好是用配置文件的方式:

celery --config=celeryconfig --loglevel=INFO

BROKER_URL = 'amqp://'

CELERY_RESULT_BACKEND = 'amqp://'

CELERY_TASK_SERIALIZER = 'json'

CELERY_RESULT_SERIALIZER = 'json'

CELERY_TIMEZONE = 'America/Los_Angeles'

CELERY_ENABLE_UTC = True

CELERY_IMPORTS = ("tasks",)

注释下:

文章的原文,blog.xiaorui.cc

大家觉得文章对你有些作用!

如果想赏钱,可以用微信扫描下面的二维码,感谢!

另外再次标注博客原地址 xiaorui.cc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 3.6.8中使用Celery实现异步类函数,您需要按照以下步骤进行设置: 1. 首先,确保已经安装了Celery和Redis(或其他消息代理)。 ``` pip install celery redis ``` 2. 创建一个Django项目,并在项目目录下创建一个名为`tasks.py`的文件。 3. 在`tasks.py`文件中,导入Celery并创建一个Celery实例。 ```python from celery import Celery app = Celery('your_project_name') ``` 4. 在Django项目的settings.py文件中添加Celery配置。 ```python CELERY_BROKER_URL = 'redis://localhost:6379' CELERY_RESULT_BACKEND = 'redis://localhost:6379' ``` 这里使用了Redis作为消息代理和结果后端,您可以根据需要选择其他选项。 5. 在`tasks.py`文件中定义您的异步任务。 ```python @app.task def your_async_task(*args, **kwargs): # 执行异步任务的代码逻辑 pass ``` 6. 在您的异步类方法中使用`@app.task`装饰器。 ```python class YourAsyncClass: @app.task def async_method(self, *args, **kwargs): # 异步方法的代码逻辑 pass ``` 7. 在您的应用程序中,通过调用`your_async_task.delay()`或`YourAsyncClass().async_method.delay()`来调度异步任务。 ```python from your_project_name.tasks import your_async_task from your_app_name.your_module import YourAsyncClass # 调度异步任务 your_async_task.delay(*args, **kwargs) # 调度异步类方法 YourAsyncClass().async_method.delay(*args, **kwargs) ``` 8. 启动Celery工作进程。在终端中,导航到您的项目目录并运行以下命令: ``` celery -A your_project_name worker --loglevel=info ``` 这将启动一个Celery工作进程,准备接收和执行异步任务。 现在,当您调度异步任务,它将在后台执行,而不会阻塞主线程。请确保您的Celery工作进程正在运行,并根据需要进行其他配置和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值