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

配置用户和权限

[[email protected] soft]# rabbitmqctl add_user my_user my_password

Creating user "my_user" ...

...done.

[[email protected] soft]# rabbitmqctl add_vhost my_vhost

Creating vhost "my_vhost" ...

...done.

[[email protected] app]# rabbitmqctl set_permissions -p my_vhost my_user ".*" ".*" ".*"

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

...done.

[[email protected] ~]# rabbitmqctl set_user_tags my_user administrator

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

...done.

[[email protected] ~]#

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

[[email protected] ~]# 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.

[[email protected] ~]#

管理访问

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.测试程序

[[email protected] app]# more tasks.py

from celery import Celery

app = Celery(‘tasks‘, broker=‘amqp://my_user:[email protected]/my_vhost‘, backend=‘amqp‘)

@app.task

def add(x,y):

return x + y

[[email protected] app]#

[[email protected] app]#

启动worker

[[email protected] 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.‘)

-------------- [email protected] 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] [email protected] ready.

调用任务

[[email protected] 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输出

[[email protected] 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.‘)

-------------- [email protected] 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] [email protected] 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

时间: 2024-09-29 04:14:35

flask celery 安装说明的相关文章

python Flask离线安装与测试

Flask是用python进行web开发时,常见的python web框架.如果服务器可以连接到外网,可以简单的用 pip install Flask 直接将Flask的依赖包及Flask直接安装好.但公司的测试环境,一般是内网,不能随表上连到外网,这就需要采用离线软件包方式安装了.下面是我的测试安装过程. 1. 确定依赖的包(参考:http://stackoverflow.com/questions/18951829/how-to-install-flask-offline): You nee

flask celery 使用方法

一.安装 由于celery4.0不支持window,如果在window上安装celery4.0将会出现下面的错误flask_clery 你现在只能安装pip install celery==3.1 二.安装py for redis 模块 pip install redis 三.安装redis服务 网上很多文章都写得模棱两可,把人坑的不要不要的!!! Redis对于Linux是官方支持的,但是不支持window,网上很多作者写文章都不写具体的系统环境,大多数直接说pip install redis

Flask实战第67天:Flask+Celery实现邮件和短信异步发送

之前在项目中我们发送邮件和 短信都是阻塞的,现在我们来利用Celery来优化它们 官方使用文档: http://flask.pocoo.org/docs/1.0/patterns/celery/ redis服务器及插件,还有cerely在上节我们已经安装好,这里就不重复过程了. 首先,来完成邮件 在项目下新建tasks.py from flask import Flask import config from celery import Celery from flask_mail import

Python flask虚拟环境安装

1.安装virtualenv 2.在当前路径下创建文件夹,启动虚拟环境 3.在使用虚拟环境前需激活,前面出现(env说明在虚拟环境中).虚拟环境中默认安装了pip,所以直接pip安装flask 4.在虚拟路径下创建py文件测试,deactivate退出虚拟环境 #!/usr/bin/env python from flask import Flask app = Flask(__name__) @app.route('/') def hello_flask(): return "hello fl

python3.6虚拟环境以及flask的安装(常见问题)

准备基于python进行web应用开发 Python3.3以上的版本通过venv模块原生支持虚拟环境,可以代替Python之前的virtualenv. 该venv模块提供了创建轻量级"虚拟环境",提供与系统Python的隔离支持.每一个虚拟环境都有其自己的Python二进制(允许有不同的Python版本创作环境),并且可以拥有自己独立的一套Python包. 需要注意的是,在Python3.3中使用"venv"命令创建的环境不包含"pip",你需要

Python 的 Flask 框架安装应用

Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 ,使用 BSD 授权. Flask也被称为 "microframework" ,因为它使用简单的核心,用 extension 增加其他功能.Flask没有默认使用的数据库.窗体验证工具.然而,Flask保留了扩增的弹性,可以用Flask-extension加入这些功能:ORM.窗体验证工具.文件上传.各种开放式身份验证技术.最新版本为0.12.

Flask 下安装Mysql

1. 安装Mysql # sudo apt-get install mysql-server 安装过程需要输入root密码,这个密码是mysql 的root密码,之后连接mysql会用到,这个要记住. 安装完成之后,可以使用如下命令check是否安装成功. mysql --user=root -p 输入mysql 的root密码.如果可以登录成功,说明安装完成. 2. 安装 python-mysqldb sudo apt-get install python-mysqldb 这一步我遇到3个问题

Flask AppBuilder安装

安装 安装很简单,使用普通的python软件包安装.我建议您另外安装基础骨架应用程序,以便您可以立即运行应用程序(没有任何型号)和易于增长的样板. 简单安装 您可以通过以下方式安装框架: $ pip install flask-appbuilder 建议虚拟环境安装 虚拟环境是非常可取的,因为您拥有的项目越多,您将越有可能使用不同版本的Python本身,或至少不同版本的Python库.让我们面对这一点:图书馆通常往往兼容兼容,而且任何严重的应用程序都不可能有零依赖关系.那么如果两个或更多的项目有

flask celery 的神坑

一.flask运行在debug模式的时候,celery无法收到flask中发送给celery的异步任务 run.py if __name__ == '__main__': # app.run(host="0.0.0.0", port=8000, debug=True) # 以debug模式运行flask # 使用debug模式时,celery异步任务不能执行,但定时任务可以执行 app.run(host="0.0.0.0", port=8000) task.py f