12 funboost 控制台支持命令行

funboost 2023.11 新增支持命令行,启动消费 发布消息 清空消息 暂停消费等功能

有些人喜欢如 scrapy celery 这样的框架,在命令行敲击一长串命令来灵活启动python任务,

本人非常讨厌这样的框架,控制台命令行不能代码补全,敲击难,感觉是鸡肋

但是有的小伙伴喜欢这样的命令行方式来启动python,现在funboost加上命令行功能.

用户只需要@boost定义消费函数定义f1,f2, 在命令行指定启动哪些queues 就行了,
而不是先需要在脚本中写好 f1.consume() f2.consume(),然后再启动这个脚本.

python 项目根目录/funboost_cli_user.py --help 可以查看有哪些命令

12.0 funboost命令行使用fire实现

funboost命令行使用fire实现的

fire很好用,很方便,比任何命令行都好用,建议大家以后开发命令行工具使用fire,不要使用argparse和click

12.1 命令行分为调用funboost包内命令和用户自身项目的命令行

12.1.1 funboost自身命令行,python -m funboost 是自动调用 funboost的 main.py 的fire命令行

12.1.2 用户项目根目录下的 funboost_cli_user.py 的文件可以接受命令行传参

python -m funboost 和 python 项目根目录/funboost_cli_user.py 是一样的个功能

funboost_cli_user.py是首次启动项目自动把funboost/core/cli/funboost_cli_user_templ.py复制到用户项目根目录下的

用户可以在funboost_cli_user.py里面灵活加代码,这样在调用命令行就能少传参 --project_root_path 和 --booster_dirs_str 了

所以建议用户使用 python 项目根目录/funboost_cli_user.py 的命令行,而不是使用 python -m funboost

说明:之后的例子不再同时列举 python -m funboos 和 python funboost_cli_user.py

12.1.3 python -m funboost 和 python 项目根目录/funboost_cli_user.py 传参不同点

python -m funboost  必须传递 --project_root_path=你的项目根目录
而且在敲击 python -m funboost 之前需要用户先设置临时环境变量 set/export PYTHONPATH=用户项目根目录 (因为nb_log需要先读取配置文件)


python 你的项目根目录/funboost_cli_user.py 命令行不需要传参指定--project_root_path ,也不需要先设置环境变量
因为funboost_cli_user.py就在用户项目根目录,所以代码中自动添加了当前项目根目录到sys.path 和指定project_root_path为当前项目根目录,
此外funboost_cli_user.py中用户可以import 消费函数所在模块,
也可以 BoosterDiscovery(project_root_path, booster_dirs=['需要扫描的消费函数所在文件夹'], max_depth=1,py_file_re_str=None).auto_discovery()来自动import发现

这就是建议用户使用 python funboost_cli_user.py xxxxx 这样来调用命令行而不是python -m funboost xxx 来调用命令行

12.2 funboost命令行指定消费函数所在的模块或文件夹

用户如果没import 消费函数所在模块或者调用 BoosterDiscovery.auto_discovery, 需要在命令行传参.

如果需要导入多个模块,import_modules_str的值如果多个模块需使用逗号隔开
python funboost_cli_user.py --import_modules_str "test_frame.test_funboost_cli.def_tasks3"  publish test_cli3_queue "{'x':3,'y':4}"
    
如果没有亲自import boost函数所在模块,则可以自动扫描文件夹下的py文件,自动import,如是果多个文件夹用,隔开
python funboost_cli_user.py --boost_dirs_str 'test_find_boosters,test_find_booster2'  push test_find_queue1 --x=1 --y=2

12.3 打印发现的用户定义的@boost消费函数

show_all_queues

python -m funboost  --project_root_path=用户项目根目录   --booster_dirs_str=文件夹1,文件夹2 --max_depth=2  show_all_queues (需要先set/export PYTHONPATH=用户项目根目录)


 python funboost_cli_user.py -booster_dirs_str=test_frame/test_funboost_cli/test_find_boosters --max_depth=2 show_all_queues

如果 funboost_cli_user.py 加了 BoosterDiscovery(project_root_path, booster_dirs=['文件夹1','文件夹2'], max_depth=2,py_file_re_str=None).auto_discovery(),那么写
python funboost_cli_user.py show_all_queues  即可.

用户可以拉取funboost项目中的自带的测试例子来测试命令行

python funboost_cli_user.py  --booster_dirs_str=test_frame/test_funboost_cli/test_find_boosters --max_depth=2  show_all_queues 

12.4 funboost命令行清空消息队列

clear

python funboost_cli_user.py clear  queue1  queue2   # 清空消息队列queue1和queue2,多个队列用空格隔开就行

12.5 funboost命令行给一个队列发布消息

push 或 publish

python funboost_cli_user.py push test_cli1_queue 1 2  # 发布消息
python funboost_cli_user.py push test_cli1_queue 1 --y=2 # 发布消息,也可以明显点传入参名字
python funboost_cli_user.py publish test_cli1_queue "{'x':3,'y':4}"  # 发布消息传递一个字典
python funboost_cli_user.py publish test_cli1_queue '{"x":3,"y":4}' # 错误方式

12.6 funboost命令行启动多个queue消费者

当前进程内启动多个conusmer consume,

每个queue使用多个进程启动消费 m_consume

python funboost_cli_user.py consume test_cli1_queue test_cli2_queue  # 启动两个队列的函数消费
python funboost_cli_user.py m_consume --test_cli1_queue=2 --test_cli2_queue=3 # 叠加多进程启动消费,test_cli1_queue启动2进程,test_cli2_queue启动3进程.

12.6.b funboost命令行启动所有queue消费者,用户无需指定队列名

当前进程内启动多个conusmer consume_all_queues 或 consume_all 每个queue使用多个进程启动消费 multi_process_consume_all_queues $process_num 或 m_consume_all $process_num

python funboost_cli_user.py consume_all
python funboost_cli_user.py m_consume_all 2

12.7 funboost命令行暂停消费

pause

支持暂停,前提是 @boost指定 is_send_consumer_hearbeat_to_redis=True

python funboost_cli_user.py pause queue1  queue2    #queue1  queue2 两个队列暂停消费