MacOS 安装celery、rabbitmq、flower,执行任务并监控任务状态
安装Celery
使用pip安装
pip install celery
定义Celery任务
根据celery官网教程:http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#first-steps
1. 开启命令行窗口1,在家目录下创建文件task.py => vi tasks.py
2. 在tasks.py下,写有关任务的代码:
from celery import Celery
app = Celery('tasks', broker='pyamqp://[email protected]//')
@app.task
def add(x, y):
return x + y
3. 保存文件 => wq!
安装并启动消息中间件RabbitMQ
可以使用homebrew安装
brew install rabbitmq
也可以使用官网上面的Standalone MacOS Build安装:http://www.rabbitmq.com/download.html
下载rabbit-server-mac-standalone-3.6.7.tar.xz,执行xz rabbit-server-mac-standalone-3.6.7.tar.xz,将xz文件解压成tar文件
继续执行tar -zxvf rabbit-server-mac-standalone-3.6.7.tar 解压成rabbit-server-mac-standalone-3.6.7
在usr/local目录下创建sbin目录,在sbin目录下创建rabbit-server-mac-standalone-3.6.7目录,将Downloads目录中解压好的rabbit-server-mac-standalone-3.6.7目录下的内容全部复制到usr/local/sbin/rabbit-server-mac-standalone-3.6.7目录下:
cp -r /Users/whatever_your_host_name/Downloads/rabbitmq_server-3.6.7/* /usr/local/sbin/rabbitmq_server-3.6.7/
设置环境变量,在.bash_profile文件中加入对应的目录:
PATH=$PATH:/usr/local/sbin/rabbitmq_server-3.6.7/sbin
执行source .bash_profile使环境变量的修改立即生效
开启命令行窗口2,启动RabbitMQ:
sudo rabbitmq-server
显示如下,则代表启动成功:
RabbitMQ 3.6.7. Copyright (C) 2007-2016 Pivotal Software, Inc.
## ## Licensed under the MPL. See http://www.rabbitmq.com/
## ##
########## Logs: /usr/local/sbin/rabbitmq_server-3.6.7/var/log/rabbitmq/[email protected]
###### ## /usr/local/sbin/rabbitmq_server-3.6.7/var/log/rabbitmq/[email protected]
##########
Starting broker...
completed with 6 plugins.
查看RabbitMQ节点状态
sudo rabbitmqctl status
若显示如下代表启动成功:
Status of node [email protected] ...
[{pid,30454},
{running_applications,
[{rabbitmq_management,"RabbitMQ Management Console","3.6.7"},
{rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.6.7"},
{rabbitmq_management_agent,"RabbitMQ Management Agent","3.6.7"},
{rabbit,"RabbitMQ","3.6.7"},
{os_mon,"CPO CXC 138 46","2.3.1"},
{ssl,"Erlang/OTP SSL application","6.0"},
{cowboy,"Small, fast, modular HTTP server.","1.0.3"},
{public_key,"Public key infrastructure","0.23"},
{cowlib,"Support library for manipulating Web protocols.","1.0.1"},
{crypto,"CRYPTO","3.5"},
{amqp_client,"RabbitMQ AMQP Client","3.6.7"},
{rabbit_common,
"Modules shared by rabbitmq-server and rabbitmq-erlang-client",
"3.6.7"},
{inets,"INETS CXC 138 49","5.10.6"},
{mnesia,"MNESIA CXC 138 12","4.12.5"},
{compiler,"ERTS CXC 138 10","5.0.4"},
{xmerl,"XML parser","1.3.7"},
{syntax_tools,"Syntax tools","1.6.18"},
{asn1,"The Erlang ASN1 compiler version 3.0.4","3.0.4"},
{ranch,"Socket acceptor pool for TCP protocols.","1.2.1"},
{sasl,"SASL CXC 138 11","2.4.1"},
{stdlib,"ERTS CXC 138 10","2.4"},
{kernel,"ERTS CXC 138 10","3.2"}]},
{os,{unix,darwin}},
{erlang_version,
"Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:64] [hipe] [kernel-poll:true]\n"},
{memory,
[{total,67263760},
{connection_readers,61192},
{connection_writers,17856},
{connection_channels,129352},
{connection_other,209720},
{queue_procs,225664},
{queue_slave_procs,0},
{plugins,3258600},
{other_proc,23639248},
{mnesia,71248},
{metrics,191600},
{mgmt_db,516328},
{msg_index,97952},
{other_ets,2779576},
{binary,1746024},
{code,27420642},
{atom,992409},
{other_system,6095141}]},
{alarms,[]},
{listeners,[{clustering,25672,"::"},{amqp,5672,"::"},{http,15672,"::"}]},
{vm_memory_high_watermark,0.4},
{vm_memory_limit,2985777561},
{disk_free_limit,50000000},
{disk_free,47682379776},
{file_descriptors,
[{total_limit,156},{total_used,6},{sockets_limit,138},{sockets_used,3}]},
{processes,[{limit,1048576},{used,365}]},
{run_queue,0},
{uptime,1442},
{kernel,{net_ticktime,60}}]
启动celery worker
开启命令行窗口3,执行:
celery -A tasks worker --loglevel=info
若显示如下代表启动成功:
[2018-10-27 16:53:06,485: WARNING/MainProcess] /anaconda3/envs/snakes/lib/python2.7/site-packages/celery/apps/worker.py:161: CDeprecationWarning:
Starting from version 3.2 Celery will refuse to accept pickle by default.
The pickle serializer is a security concern as it may give attackers
the ability to execute any command. It's important to secure
your broker from unauthorized access when using pickle, so we think
that enabling pickle should require a deliberate action and not be
the default choice.
If you depend on pickle then you should set a setting to disable this
warning and to be sure that everything will continue working
when you upgrade to Celery 3.2::
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
You must only enable the serializers that you will actually use.
warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED))
-------------- [email protected] v3.1.18 (Cipater)
---- **** -----
--- * *** * -- Darwin-18.0.0-x86_64-i386-64bit
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x1028a85d0
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: disabled
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery
[tasks]
. tasks.add
[2018-10-27 16:53:06,530: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2018-10-27 16:53:06,544: INFO/MainProcess] mingle: searching for neighbors
[2018-10-27 16:53:07,557: INFO/MainProcess] mingle: all alone
[2018-10-27 16:53:07,570: WARNING/MainProcess] [email protected] ready.
安装Flower
使用pip安装:
pip install flower
启动flower:
flower --port=5555
调用任务(Calling the task)
切换到命令行窗口1
>>> from tasks import add
>>> add.delay(4, 4)
若结果如下,则代表任务执行成功:
<AsyncResult: 53b79746-2f76-488e-b026-c157313d6cc8>
此时切换到命令行窗口3,显示如下:
[2018-10-27 16:55:55,800: INFO/MainProcess] Received task: tasks.add[53b79746-2f76-488e-b026-c157313d6cc8]
[2018-10-27 16:55:55,802: INFO/MainProcess] Task tasks.add[53b79746-2f76-488e-b026-c157313d6cc8] succeeded in 0.000842250999995s: 8
可以看到任务的结果为8,符合预期。
查看监控信息
浏览器访问:http://localhost:5555/
看到如下界面:
Processed值:1
Succeeded值:1