Django 项目中 Celery task Debug 调试

celery debug 调试直接用django 不是很方便

可以采用 pdb 的形式进行调试,在celery 中她对 pdb 做了一次的封装,变成了rdb

引入rdb

Python
from <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/celery" title="View all posts in celery" target="_blank">celery</a></span>.contrib import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/rdb" title="View all posts in rdb" target="_blank">rdb</a></span> #引入包 rdb.set_trace() # 设置断点
1
2
3
4
from celery.contrib import rdb #引入包
 
rdb.set_trace() # 设置断点
 

启动django

Python
Django version 2.1.4, using settings 'taskproj.settings' Development server is running at http://[127.0.0.1]:8000/ Using the Werkzeug debugger (http://werkzeug.pocoo.org/) Quit the server with CONTROL-C. * Debugger is active! * Debugger PIN: 329-253-017 127.0.0.1 - - [03/Jul/2019 16:36:03] "POST /start_task/ HTTP/1.1" 200 -
1
2
3
4
5
6
7
8
Django version 2.1.4, using settings 'taskproj.settings'
Development server is running at http://[127.0.0.1]:8000/
Using the Werkzeug debugger (http://werkzeug.pocoo.org/)
Quit the server with CONTROL-C.
* Debugger is active!
* Debugger PIN: 329-253-017
127.0.0.1 - - [03/Jul/2019 16:36:03] "POST /start_task/ HTTP/1.1" 200 -
 

启动worker

Python
. app01.tasks.add . app01.tasks.get_url . app01.tasks.mul . app01.utils.tasks.C . app01.utils.tasks.extract_data . app01.utils.tasks.extract_urls . app01.utils.tasks.get_contents . app01.utils.tasks.parser . app01.utils.tasks.xpath_extract_urls . celery.accumulate . celery.backend_cleanup . celery.chain . celery.chord . celery.chord_unlock . celery.chunks . celery.group . celery.map . celery.starmap . taskproj.celery.debug_task
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  . app01.tasks.add
  . app01.tasks.get_url
  . app01.tasks.mul
  . app01.utils.tasks.C
  . app01.utils.tasks.extract_data
  . app01.utils.tasks.extract_urls
  . app01.utils.tasks.get_contents
  . app01.utils.tasks.parser
  . app01.utils.tasks.xpath_extract_urls
  . celery.accumulate
  . celery.backend_cleanup
  . celery.chain
  . celery.chord
  . celery.chord_unlock
  . celery.chunks
  . celery.group
  . celery.map
  . celery.starmap
  . taskproj.celery.debug_task
 
 

发送生产者

Python
<span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span> post_data.py
1
2
python post_data.py
 

观察worker

Python
-de13-4aca-8cb9-22f32ea5d00f', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'retries': 0, 'timelimit': [None, None], 'root_id': '8461613f-18ab-4756-8fff-e56c164613b1', 'parent_ id': '8461613f-18ab-4756-8fff-e56c164613b1', 'argsrepr': "(['https://www.csai.cn/gonglue/', 'https://www.csai.cn/pingce/', 'https://jin.baidu.com/article', 'https://jin.baidu.com/article/car', 'https://jin.baidu.com/article/list_120 000', 'https://jin.baidu.com/article/list_160000'],)", 'kwargsrepr': '{}', 'origin': '[email protected]', 'reply_to': 'd9fda1c9-044d-38e5-b640-d7f418f525b3', 'correlation_id': '7515aa8e-de13-4aca-8cb9-22f32ea5d00f', 'delivery_info ': {'exchange': '', 'routing_key': 'celery', 'priority': 0, 'redelivered': False}}, '[[["https://www.csai.cn/gonglue/", "https://www.csai.cn/pingce/", "https://jin.baidu.com/article",... kwargs:{}) [2019-07-04 09:18:46,773: DEBUG/MainProcess] Task accepted: app01.utils.tasks.xpath_extract_urls[7515aa8e-de13-4aca-8cb9-22f32ea5d00f] pid:15575 [2019-07-04 09:18:46,776: WARNING/ForkPoolWorker-2] Remote Debugger:6901: Ready to connect: telnet 127.0.0.1 6901 Type `exit` in session to continue. Remote Debugger:6901: Waiting for client... # 监听的是 6901
1
2
3
4
5
6
7
8
9
10
11
-de13-4aca-8cb9-22f32ea5d00f', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'retries': 0, 'timelimit': [None, None], 'root_id': '8461613f-18ab-4756-8fff-e56c164613b1', 'parent_
id': '8461613f-18ab-4756-8fff-e56c164613b1', 'argsrepr': "(['https://www.csai.cn/gonglue/', 'https://www.csai.cn/pingce/', 'https://jin.baidu.com/article', 'https://jin.baidu.com/article/car', 'https://jin.baidu.com/article/list_120
000', 'https://jin.baidu.com/article/list_160000'],)", 'kwargsrepr': '{}', 'origin': 'gen15574@songhao-pc', 'reply_to': 'd9fda1c9-044d-38e5-b640-d7f418f525b3', 'correlation_id': '7515aa8e-de13-4aca-8cb9-22f32ea5d00f', 'delivery_info
': {'exchange': '', 'routing_key': 'celery', 'priority': 0, 'redelivered': False}}, '[[["https://www.csai.cn/gonglue/", "https://www.csai.cn/pingce/", "https://jin.baidu.com/article",... kwargs:{})
[2019-07-04 09:18:46,773: DEBUG/MainProcess] Task accepted: app01.utils.tasks.xpath_extract_urls[7515aa8e-de13-4aca-8cb9-22f32ea5d00f] pid:15575
[2019-07-04 09:18:46,776: WARNING/ForkPoolWorker-2] Remote Debugger:6901: Ready to connect: telnet 127.0.0.1 6901
 
Type `exit` in session to continue.
 
Remote Debugger:6901: Waiting for client... # 监听的是 6901
 

打开监听端口 进行调试

Django 项目中 Celery task Debug 调试

rdb 调试操作

Django 项目中 Celery task Debug 调试

Django 项目中 Celery task Debug 调试