flask celery 安装说明

简介:

操作系统环境

CentOS 7.4 X64


  1. rabbitmq-server


# yum install -y epel-release

# yum install erlang

# yum install -y rabbitmq-server


也可以添加-detached属性来后台运行


rabbitmq-server -detached



不要kill停止RabbitMQ,使用rabbitmqctl命令


rabbitmqctl stop



配置用户和权限


[root@localhost soft]# rabbitmqctl add_user my_user my_password

Creating user "my_user" ...

...done.

[root@localhost soft]# rabbitmqctl add_vhost my_vhost

Creating vhost "my_vhost" ...

...done.

[root@localhost app]# rabbitmqctl set_permissions -p my_vhost my_user ".*" ".*" ".*"

Setting permissions for user "my_user" in vhost "my_vhost" ...

...done.


[root@localhost ~]# rabbitmqctl set_user_tags my_user administrator

Setting tags for user "my_user" to [administrator] ...

...done.

[root@localhost ~]#


配置web管理界面(需要重启服务)


[root@localhost ~]# rabbitmq-plugins enable rabbitmq_management

The following plugins have been enabled:

  mochiweb

  webmachine

  rabbitmq_web_dispatch

  amqp_client

  rabbitmq_management_agent

  rabbitmq_management

Plugin configuration has changed. Restart RabbitMQ for changes to take effect.

[root@localhost ~]# 


管理访问


http://rabbitmqserver_ip:15672/


使用上述用户登录查看队列情况



2.安装python库


# yum install -y python-pip

# pip install --upgrade pip

# pip --version

pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)


# pip install Flask

# pip install celery



3.测试程序


[root@localhost app]# more tasks.py

from celery import Celery 

app = Celery('tasks', broker='amqp://my_user:my_password@localhost/my_vhost', backend='amqp')  


@app.task 

def add(x,y):

    return x + y

[root@localhost app]# 

[root@localhost app]#




启动worker


[root@localhost app]# celery -A tasks worker --loglevel=info

/usr/lib/python2.7/site-packages/celery/platforms.py:795: RuntimeWarning: You're running the worker with superuser privileges: this is

absolutely not recommended!


Please specify a different user using the -u option.


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


  uid=uid, euid=euid, gid=gid, egid=egid,

/usr/lib/python2.7/site-packages/celery/backends/amqp.py:68: CPendingDeprecationWarning: 

    The AMQP result backend is scheduled for deprecation in     version 4.0 and removal in version v5.0.     Please use RPC backend or a persistent backend.


  alternative='Please use RPC backend or a persistent backend.')

 

 -------------- celery@localhost.localdomain v4.1.0 (latentcall)

---- **** ----- 

--- * ***  * -- Linux-3.10.0-693.el7.x86_64-x86_64-with-centos-7.4.1708-Core 2017-11-06 17:43:36

-- * - **** --- 

- ** ---------- [config]

- ** ---------- .> app:         tasks:0x2099210

- ** ---------- .> transport:   amqp://my_user:**@localhost:5672/my_vhost

- ** ---------- .> results:     amqp://

- *** --- * --- .> concurrency: 1 (prefork)

-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)

--- ***** ----- 

 -------------- [queues]

                .> celery           exchange=celery(direct) key=celery

                


[tasks]

  . tasks.add


[2017-11-06 17:43:36,345: INFO/MainProcess] Connected to amqp://my_user:**@127.0.0.1:5672/my_vhost

[2017-11-06 17:43:36,366: INFO/MainProcess] mingle: searching for neighbors

[2017-11-06 17:43:37,398: INFO/MainProcess] mingle: all alone

[2017-11-06 17:43:37,435: INFO/MainProcess] celery@localhost.localdomain ready.




调用任务



[root@localhost app]# python

Python 2.7.5 (default, Aug  4 2017, 00:39:18) 

[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> 

>>> from tasks import add

>>> result = add.delay(3, 5)

>>> result.ready()

True

>>> result.get()

8

>>>



work输出



[root@localhost app]# celery -A tasks worker --loglevel=info

/usr/lib/python2.7/site-packages/celery/platforms.py:795: RuntimeWarning: You're running the worker with superuser privileges: this is

absolutely not recommended!


Please specify a different user using the -u option.


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


  uid=uid, euid=euid, gid=gid, egid=egid,

/usr/lib/python2.7/site-packages/celery/backends/amqp.py:68: CPendingDeprecationWarning: 

    The AMQP result backend is scheduled for deprecation in     version 4.0 and removal in version v5.0.     Please use RPC backend or a persistent backend.


  alternative='Please use RPC backend or a persistent backend.')

 

 -------------- celery@localhost.localdomain v4.1.0 (latentcall)

---- **** ----- 

--- * ***  * -- Linux-3.10.0-693.el7.x86_64-x86_64-with-centos-7.4.1708-Core 2017-11-06 17:43:36

-- * - **** --- 

- ** ---------- [config]

- ** ---------- .> app:         tasks:0x2099210

- ** ---------- .> transport:   amqp://my_user:**@localhost:5672/my_vhost

- ** ---------- .> results:     amqp://

- *** --- * --- .> concurrency: 1 (prefork)

-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)

--- ***** ----- 

 -------------- [queues]

                .> celery           exchange=celery(direct) key=celery

                


[tasks]

  . tasks.add


[2017-11-06 17:43:36,345: INFO/MainProcess] Connected to amqp://my_user:**@127.0.0.1:5672/my_vhost

[2017-11-06 17:43:36,366: INFO/MainProcess] mingle: searching for neighbors

[2017-11-06 17:43:37,398: INFO/MainProcess] mingle: all alone

[2017-11-06 17:43:37,435: INFO/MainProcess] celery@localhost.localdomain ready.

------------------------------------------- new output ----------------------------------------------------

[2017-11-06 17:45:31,929: INFO/MainProcess] Received task: tasks.add[4c381059-b943-4a4c-8975-15505674504f]  

[2017-11-06 17:45:31,982: INFO/ForkPoolWorker-1] Task tasks.add[4c381059-b943-4a4c-8975-15505674504f] succeeded in 0.0510413989978s: 8




本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1979393
相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
4月前
|
Linux iOS开发 MacOS
Flask 安装
Flask 安装还是比较简单的。
93 18
|
5月前
|
NoSQL Redis UED
揭秘!Flask如何携手Celery,让异步任务处理不再是难题,打造极速响应的Web应用新纪元!
【8月更文挑战第31天】在Web开发中,Flask与Celery的结合为异步任务处理提供了强大支持。Flask作为轻量级Web框架,以其简洁灵活著称;而Celery则是一个分布式任务队列系统,擅长处理耗时任务。二者结合,Flask专注于处理Web请求,Celery则在后台异步执行复杂任务,如发送邮件或调用外部API。这种方式不仅提升了应用性能和响应速度,还优化了用户体验。下面的示例展示了如何在Flask应用中集成Celery以实现异步任务处理。
199 0
|
4月前
|
NoSQL 大数据 Redis
使用 Flask 和 Celery 构建异步任务处理
使用 Flask 和 Celery 构建异步任务处理
128 2
|
4月前
|
数据采集 NoSQL 调度
flask celery python 每月定时任务
flask celery python 每月定时任务
|
5月前
|
数据库 关系型数据库 MySQL
惊!Hibernate与MySQL的绝密优化技巧大揭秘,让你的数据库飞起来!
【8月更文挑战第31天】在企业应用开发中,结合使用持久层框架Hibernate与数据库管理系统MySQL可显著提升数据库交互效率。本文探讨了多项优化策略,包括配置二级缓存、采用单向关联减少JOIN操作、优化HQL查询语句以及合理使用MySQL索引。通过具体示例,文章详细讲解了如何实施这些优化措施,以期为企业应用提供更高效稳定的数据支持。
73 0
|
6月前
|
JSON 数据格式 Python
Flask实现内部接口----pycharm安装及新建,location代表着文件路径,下面是Python的环境,Flask是由Python开发的框架,Python文件接口ython通过GET发送
Flask实现内部接口----pycharm安装及新建,location代表着文件路径,下面是Python的环境,Flask是由Python开发的框架,Python文件接口ython通过GET发送
|
8月前
|
监控 NoSQL 测试技术
python使用Flask,Redis和Celery的异步任务
python使用Flask,Redis和Celery的异步任务
|
8月前
|
安全 搜索推荐 前端开发
如何在本地安装Flask并将其web界面发布到公网上远程访问协同开发
如何在本地安装Flask并将其web界面发布到公网上远程访问协同开发
|
前端开发 JavaScript 数据库
【100天精通Python】Day49:Python Web编程_Web框架,Flask框架从安装到使用
【100天精通Python】Day49:Python Web编程_Web框架,Flask框架从安装到使用
88 0
|
Python
一分钟学会Flask框架的安装与快速使用
一分钟学会Flask框架的安装与快速使用
171 0