root@b792ae940e3e:/app# dpkg -i rabbitmq-server_3.7.0-1_all.deb Selecting previously unselected package rabbitmq-server. (Reading database ... 7744 files and directories currently installed.) Preparing to unpack rabbitmq-server_3.7.0-1_all.deb ... Unpacking rabbitmq-server (3.7.0-1) ... dpkg: dependency problems prevent configuration of rabbitmq-server: rabbitmq-server depends on erlang-nox (>= 1:19.3) | esl-erlang (>= 1:19.3); however: Package erlang-nox is not installed. Package esl-erlang is not installed. rabbitmq-server depends on logrotate; however: Package logrotate is not installed. rabbitmq-server depends on socat; however: Package socat is not installed.
dpkg: error processing package rabbitmq-server (--install): dependency problems - leaving unconfigured Processing triggers for systemd (229-4ubuntu21) ... Errors were encountered while processing: rabbitmq-server root@b792ae940e3e:/app# apt-get -f install Reading package lists... Done Building dependency tree Reading state information... Done Correcting dependencies... Done The following packages will be REMOVED: rabbitmq-server 0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded. 1 not fully installed or removed. After this operation, 13.3 MB disk space will be freed. Do you want to continue? [Y/n]
root@b792ae940e3e:/app# ls esl-erlang_20.1.7-1~ubuntu~xenial_amd64.deb rabbitmq-server_3.7.0-1_all.deb root@b792ae940e3e:/app# dpkg -i esl-erlang_20.1.7-1~ubuntu~xenial_amd64.deb Selecting previously unselected package esl-erlang. (Reading database ... 7744 files and directories currently installed.) Preparing to unpack esl-erlang_20.1.7-1~ubuntu~xenial_amd64.deb ... Unpacking esl-erlang (1:20.1.7) ... dpkg: dependency problems prevent configuration of esl-erlang: esl-erlang depends on libwxbase2.8-0 | libwxbase3.0-0 | libwxbase3.0-0v5; however: Package libwxbase2.8-0 is not installed. Package libwxbase3.0-0 is not installed. Package libwxbase3.0-0v5 is not installed. esl-erlang depends on libwxgtk2.8-0 | libwxgtk3.0-0 | libwxgtk3.0-0v5; however: Package libwxgtk2.8-0 is not installed. Package libwxgtk3.0-0 is not installed. Package libwxgtk3.0-0v5 is not installed. esl-erlang depends on libsctp1; however: Package libsctp1 is not installed.
dpkg: error processing package esl-erlang (--install): dependency problems - leaving unconfigured Errors were encountered while processing: esl-erlang root@b792ae940e3e:/app# apt-get -f install Reading package lists... Done Building dependency tree Reading state information... Done Correcting dependencies... Done The following additional packages will be installed: ...... ......
root@b792ae940e3e:/app# dpkg -i rabbitmq-server_3.7.0-1_all.deb Selecting previously unselected package rabbitmq-server. (Reading database ... 29232 files and directories currently installed.) Preparing to unpack rabbitmq-server_3.7.0-1_all.deb ... Unpacking rabbitmq-server (3.7.0-1) ... dpkg: dependency problems prevent configuration of rabbitmq-server: rabbitmq-server depends on logrotate; however: Package logrotate is not installed. rabbitmq-server depends on socat; however: Package socat is not installed.
dpkg: error processing package rabbitmq-server (--install): dependency problems - leaving unconfigured Processing triggers for systemd (229-4ubuntu21) ... Errors were encountered while processing: rabbitmq-server root@b792ae940e3e:/app# apt-get -f install Reading package lists... Done Building dependency tree Reading state information... Done Correcting dependencies... Done The following additional packages will be installed: cron libpopt0 libwrap0 logrotate socat tcpd Suggested packages: anacron checksecurity exim4 | postfix | mail-transport-agent mailx The following NEW packages will be installed: cron libpopt0 libwrap0 logrotate socat tcpd 0 upgraded, 6 newly installed, 0 to remove and 2 not upgraded. 1 not fully installed or removed. Need to get 522 kB of archives. After this operation, 1674 kB of additional disk space will be used. Do you want to continue? [Y/n]
root@730778dc65bd:/app# rabbitmq-plugins enable rabbitmq_management The following plugins have been enabled: mochiweb webmachine rabbitmq_web_dispatch amqp_client rabbitmq_management_agent rabbitmq_management
Applying plugin configuration to rabbit@730778dc65bd... started 6 plugins. root@730778dc65bd:/app# service rabbitmq-server restart * Restarting RabbitMQ Messaging Server rabbitmq-server [ OK ] root@730778dc65bd:/app#
## 查看rabbitmq状态 root@730778dc65bd:/app# rabbitmqctl status Status of node rabbit@730778dc65bd ... Error: unable to connect to node rabbit@730778dc65bd: nodedown
DIAGNOSTICS ===========
attempted to contact: [rabbit@730778dc65bd]
rabbit@730778dc65bd: * connected to epmd (port 4369) on 730778dc65bd * epmd reports: node 'rabbit' not running at all no other nodes on 730778dc65bd * suggestion: start the node
current node details: - node name: 'rabbitmq-cli-9223@730778dc65bd' - home dir: /var/lib/rabbitmq - cookie hash: MwPrvM8WUeAkWCiIWYw2fg==
root@730778dc65bd:/app#
root@730778dc65bd:/app# service rabbitmq-server start * Starting RabbitMQ Messaging Server rabbitmq-server [ OK ] root@730778dc65bd:/app# rabbitmqctl status Status of node rabbit@730778dc65bd ... [{pid,9527}, {running_applications,[{rabbit,"RabbitMQ","3.5.7"}, {mnesia,"MNESIA CXC 138 12","4.13.3"}, {xmerl,"XML parser","1.3.10"}, {os_mon,"CPO CXC 138 46","2.4"}, {sasl,"SASL CXC 138 11","2.7"}, {stdlib,"ERTS CXC 138 10","2.8"}, {kernel,"ERTS CXC 138 10","4.2"}]}, {os,{unix,linux}}, {erlang_version,"Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:2:2] [async-threads:64] [kernel-poll:true]\n"}, {memory,[{total,83922208}, {connection_readers,0}, {connection_writers,0},
root@b792ae940e3e:/app# celery -A tasks worker --loglevel=info /usr/local/lib/python3.5/dist-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/local/lib/python3.5/dist-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.
[2017-12-09 13:50:06,121: INFO/MainProcess] Connected to amqp://myuser:**@127.0.0.1:5672/hellohost [2017-12-09 13:50:06,137: INFO/MainProcess] mingle: searching for neighbors [2017-12-09 13:50:07,178: INFO/MainProcess] mingle: all alone [2017-12-09 13:50:07,228: INFO/MainProcess] celery@b792ae940e3e ready.
可以看到 celery 的 worker 已经准备就绪了。
查看 worker 完整的命令行参数列表:
1 2 3
$ celery worker --help ## 或者: $ celery help
调用任务
使用 delay() 方法来调用任务。
新打开一个控制台界面,
1
$ docker exec -it celery1 /bin/bash
执行:
1 2 3 4
# python3
>>> from tasks import add >>> add.delay(4, 4)
示例:
1 2 3 4 5 6 7 8
root@b792ae940e3e:/app# python3 Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from tasks import add >>> add.delay(3,4) <AsyncResult: e1ae8ea3-8a8f-47c5-befb-e6ba975f0580> >>>
执行结果:
同时,也可以在 RabbitMQ web 管理页面看到新增了一个任务并存储了处理结果:
为了得到调用任务后返回的 AsyncResult 实例,通过一个参数来接收:
1
>>> result=add.delay(3,4)
ready() 方法查看任务是否完成处理:
1 2
>>> result.ready() True #结果返回 `True` 表示任务处理完成
这里是异步调用,如果我们需要返回的结果,那么要等 ready 状态为 True 才行。
执行结果:
1 2 3 4 5
[2017-12-09 13:50:07,228: INFO/MainProcess] celery@b792ae940e3e ready. [2017-12-09 14:00:33,132: INFO/MainProcess] Received task: tasks.add[e1ae8ea3-8a8f-47c5-befb-e6ba975f0580] [2017-12-09 14:00:33,163: INFO/ForkPoolWorker-1] Task tasks.add[e1ae8ea3-8a8f-47c5-befb-e6ba975f0580] succeeded in 0.02956800399988424s: 7 [2017-12-09 14:17:21,033: INFO/MainProcess] Received task: tasks.add[c178619e-3af3-41ed-8d2c-6371de80a601] [2017-12-09 14:17:21,058: INFO/ForkPoolWorker-1] Task tasks.add[c178619e-3af3-41ed-8d2c-6371de80a601] succeeded in 0.024445844999718247s: 7