npm-run-all 使用实践

参考: npm-run-all

背景

在前端开发中,你是否存在以下烦恼:

  1. 写 package.json 的 scripts 命令时,命令太过冗长,例如编译命令 build 需要执行清理 clean, 编译css build:css, 编译js build:js, 编译html build:html 命令,则 build 命令需要写成 build: npm run clean && npm run build:css && npm run build:js && npm run build:html,这个命令太过冗长,npm run 都是重复的,能不能写成 build: clean build:css build:js build:html,甚至能不能更简单地写成 build: clean build:*
  2. 通常 build 的过程是先进行类型检查,后进行编译: build: npm run typecheck && vite build,两者是串行的关系,能不能使两个命令并行执行以加快编译速度?

如果你也有类似烦恼,相信这篇文章对你有用。

安装

# npm 安装
$ npm install npm-run-all --save-dev
# yarn 安装
$ yarn add npm-run-all --dev
# pnpm 安装
$ pnpm add -D npm-run-all

使用

npm-run-all 包提供了3个命令:npm-run-all, run-s, run-p

最主要的命令是 npm-run-all,我们可以使用该命令创建复杂的命令计划。run-srun-p 是简写命令,run-s 用于串行任务,run-p 用于并行任务。

npm-run-all

创建一个文件夹,并通过 pnpm init 将该文件夹初始化为一个项目。在该项目下创建4个脚本文件:

  • clean.sh:
echo "$(date): start clean"
sleep 1
echo "$(date): clean finished!"
  • build-css.sh
echo "$(date): start build:css"
sleep 1
echo "$(date): build:css finished!"
  • build-js.sh
echo "$(date): start build:js"
sleep 3
echo "$(date): build:js finished!"
  • build-html.sh
echo "$(date): start build:html"
sleep 1
echo "$(date): build:html finished!"

串行执行多个命令

为 package.json 添加脚本命令:

{"scripts": {"clean": "./clean.sh","build:css": "./build-css.sh","build:html": "./build-html.sh","build:js": "./build-js.sh","build": "npm-run-all clean build:css build:js build:html"}
}

使用 npm-run-all 后 build 命令简化了很多,执行 pnpm build 结果为:

> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all clean build:css build:js build:html> t1@1.0.0 clean /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./clean.sh2024年10月14日 星期一 17时35分17秒 CST: start clean
2024年10月14日 星期一 17时35分18秒 CST: clean finished!> t1@1.0.0 build:css /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-css.sh2024年10月14日 星期一 17时35分18秒 CST: start build:css
2024年10月14日 星期一 17时35分19秒 CST: build:css finished!> t1@1.0.0 build:js /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-js.sh2024年10月14日 星期一 17时35分19秒 CST: start build:js
2024年10月14日 星期一 17时35分22秒 CST: build:js finished!> t1@1.0.0 build:html /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-html.sh2024年10月14日 星期一 17时35分23秒 CST: start build:html
2024年10月14日 星期一 17时35分24秒 CST: build:html finished!

可以看到 clean, build:css, build:js, build:html 串行执行了。

用通配符简化命令

build 命令修改为 npm-run-all clean build:*,执行 pnpm build 结果为:

> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all clean build:*> t1@1.0.0 clean /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./clean.sh2024年10月14日 星期一 17时39分45秒 CST: start clean
2024年10月14日 星期一 17时39分46秒 CST: clean finished!> t1@1.0.0 build:css /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-css.sh2024年10月14日 星期一 17时39分46秒 CST: start build:css
2024年10月14日 星期一 17时39分47秒 CST: build:css finished!> t1@1.0.0 build:html /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-html.sh2024年10月14日 星期一 17时39分48秒 CST: start build:html
2024年10月14日 星期一 17时39分49秒 CST: build:html finished!> t1@1.0.0 build:js /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-js.sh2024年10月14日 星期一 17时39分49秒 CST: start build:js
2024年10月14日 星期一 17时39分52秒 CST: build:js finished!

可以看到 clean, build:css, build:html, build:js 串行执行了。执行 build:* 时,执行顺序按命令定义的先后顺序进行。

多个命令并行执行

build 命令修改为 npm-run-all --parallel clean build:*,执行结果为:

pnpm build> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all --parallel clean build:*> t1@1.0.0 build:css /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-css.sh> t1@1.0.0 clean /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./clean.sh> t1@1.0.0 build:html /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-html.sh> t1@1.0.0 build:js /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-js.sh2024年10月14日 星期一 17时49分47秒 CST: start build:css
2024年10月14日 星期一 17时49分47秒 CST: start build:html
2024年10月14日 星期一 17时49分47秒 CST: start clean
2024年10月14日 星期一 17时49分47秒 CST: start build:js
2024年10月14日 星期一 17时49分48秒 CST: build:html finished!
2024年10月14日 星期一 17时49分48秒 CST: build:css finished!
2024年10月14日 星期一 17时49分48秒 CST: clean finished!
2024年10月14日 星期一 17时49分50秒 CST: build:js finished!

可以看到4个命令已经并行执行了。上面 build 命令在 linux 可以写成 npm run clean & npm run build:css & npm run build:js & npm run build:html,执行结果为:

> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm run clean & npm run build:css & npm run build:js & npm run build:html> t1@1.0.0 clean
> ./clean.sh> t1@1.0.0 build:js
> ./build-js.sh> t1@1.0.0 build:html
> ./build-html.sh> t1@1.0.0 build:css
> ./build-css.sh20241014日 星期一 192114CST: start build:css
20241014日 星期一 192114CST: start build:js
20241014日 星期一 192114CST: start build:html
20241014日 星期一 192114CST: start clean
20241014日 星期一 192115CST: build:html finished!
20241014日 星期一 192115CST: build:css finished!
20241014日 星期一 192115CST: clean finished!
20241014日 星期一 192117CST: build:js finished!

也有并行执行的效果,不过问题在于 Windows 下的 cmd.exe 并不能识别 &,跨平台存在问题。

另外,因为 & 表示命令后台运行,因此并不是在 build 执行完以后才退出命令行。因为最后一条命令 npm run build:html 不是后台执行,因此命令行退出时间取决于该命令的执行时间,该命令延时1s,而执行时间最长的 npm run build:js 延时3s,因此在命令行退出时,npm run build:js 并没有执行完毕,会导致在 pnpm build 命令结束2s后才在终端输出 2024年10月14日 星期一 19时21分17秒 CST: build:js finished! 的问题。

多命令并行时一个命令执行失败

并行执行时,如果某个命令执行的退出码不是0,则正在执行的命令将被杀死。例如,我们将 build-html.sh 修改为:

echo "$(date): start build:html"
sleep 2
exit 1
echo "$(date): build:html finished!"

则执行 pnpm build 的结果为:

> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all --parallel clean build:*> t1@1.0.0 clean /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./clean.sh> t1@1.0.0 build:css /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-css.sh> t1@1.0.0 build:html /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-html.sh> t1@1.0.0 build:js /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./build-js.sh2024年10月14日 星期一 19时32分48秒 CST: start clean
2024年10月14日 星期一 19时32分48秒 CST: start build:css
2024年10月14日 星期一 19时32分48秒 CST: start build:html
2024年10月14日 星期一 19时32分48秒 CST: start build:js
2024年10月14日 星期一 19时32分49秒 CST: clean finished!
2024年10月14日 星期一 19时32分49秒 CST: build:css finished!ELIFECYCLE  Command failed with exit code 1.ELIFECYCLE  Command failed.
ERROR: "build:html" exited with 1.ELIFECYCLE  Command failed with exit code 1.

可以看到,在 build:html 错误退出前执行完毕的 clean, build:css 都输出完成信息,而执行时间比 build:html 长的 build:js 因为 build:html 的错误退出,导致无法继续执行,没有输出完成信息。

串行与并行混合执行

上面并行的例子中存在一个问题,因为 clean 和其他编译命令并行,可能导致刚编译好的文件被清理掉,我们要的结果是先执行 clean 命令,执行完成后再并行执行3个编译命令。可以将 build 命令修改为 npm-run-all --silent clean --parallel build:*,执行结果为:

> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all clean --silent --parallel build:*2024年10月14日 星期一 21时38分20秒 CST: start clean
2024年10月14日 星期一 21时38分21秒 CST: clean finished!
2024年10月14日 星期一 21时38分21秒 CST: start build:html
2024年10月14日 星期一 21时38分21秒 CST: start build:js
2024年10月14日 星期一 21时38分21秒 CST: start build:css
2024年10月14日 星期一 21时38分22秒 CST: build:html finished!
2024年10月14日 星期一 21时38分22秒 CST: build:css finished!
2024年10月14日 星期一 21时38分25秒 CST: build:js finished!

如果希望执行顺序为: 先执行 clean, 再并行执行 build:css build:js, 最后执行 build:html,可以将 build 命令改为: npm-run-all --silent clean --parallel build:css build:js --sequential build:html, 执行结果为:

> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all --silent clean --parallel build:css build:js --sequential build:html2024年10月14日 星期一 21时42分49秒 CST: start clean
2024年10月14日 星期一 21时42分50秒 CST: clean finished!
2024年10月14日 星期一 21时42分50秒 CST: start build:js
2024年10月14日 星期一 21时42分50秒 CST: start build:css
2024年10月14日 星期一 21时42分51秒 CST: build:css finished!
2024年10月14日 星期一 21时42分53秒 CST: build:js finished!
2024年10月14日 星期一 21时42分54秒 CST: start build:html
2024年10月14日 星期一 21时42分55秒 CST: build:html finished!

--sequential 可以改为 --serial

例如: npm-run-all a b --parallel c d --sequential e f --parallel g h inpm-run-all a b --parallel c d --serial e f --parallel g h i 的执行步骤为:

  1. 串行执行,先执行a,再执行b
  2. b 执行完后,并行执行 c 和 d
  3. c 和 d 都执行完后,串行执行 e 和 f
  4. f 执行完后,并行执行 g、h 和 i

通配符

之前我们看到 npm-run-all build:*,其中 * 就是一个通配符,并且分隔符不一定是冒号,例如我们将 package.json 中的 scripts 改为:

{"scripts": {"clean": "./clean.sh","build_css": "./build-css.sh","build_html": "./build-html.sh","build_js": "./build-js.sh","build": "npm-run-all --silent build_*"}
}

执行 pnpm build 的结果为:

> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all --silent build_*2024年10月14日 星期一 21时44分45秒 CST: start build:css
2024年10月14日 星期一 21时44分46秒 CST: build:css finished!
2024年10月14日 星期一 21时44分47秒 CST: start build:html
2024年10月14日 星期一 21时44分48秒 CST: build:html finished!
2024年10月14日 星期一 21时44分48秒 CST: start build:js
2024年10月14日 星期一 21时44分51秒 CST: build:js finished!

也是可以正常执行的。

甚至 * 也不一定在最后,例如 build 改为 npm-run-all --silent build_*s,将只执行 build:cssbuild:js,执行结果为:

> t1@1.0.0 build /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all --silent build_*s2024年10月14日 星期一 21时45分53秒 CST: start build:css
2024年10月14日 星期一 21时45分54秒 CST: build:css finished!
2024年10月14日 星期一 21时45分54秒 CST: start build:js
2024年10月14日 星期一 21时45分57秒 CST: build:js finished!

传入参数

新建两个脚本用于接收命令行参数:

  • foo.sh: echo "foo: [1]=$1, [2]=$2, [3]=$3, [4]=$4, [5]=$5, [6]=$6"
  • bar.sh: echo "bar: [1]=$1, [2]=$2, [3]=$3, [4]=$4, [5]=$5, [6]=$6"

为 scripts 添加如下内容:

{"scripts": {"test:foo": "./foo.sh","test:bar": "./bar.sh","test": "npm-run-all --parallel \"test:* 1 abc\"",}
}

则执行的结果为:

> t1@1.0.0 test /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all --parallel "test:* 1 abc"> t1@1.0.0 test:foo /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./foo.sh "1" "abc"> t1@1.0.0 test:bar /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./bar.sh "1" "abc"foo: [1]=1, [2]=abc, [3]=, [4]=, [5]=, [6]=
bar: [1]=1, [2]=abc, [3]=, [4]=, [5]=, [6]=

上述的 test 命令改为 "test": "npm-run-all --parallel \"test:foo 1 abc\" \"test:bar 1 abc\"" 也是相同的效果。

参数占位符

如果我们希望在执行命令时,再通过命令行传入参数,可以使用参数占位符。下面的例子演示了参数占位符的使用:

{"test2": "npm-run-all \"test:foo {1}\" \"test:foo {1} {2}\" \"test:foo {@}\" \"test:foo {*}\" --"
}

注意,最后的 -- 不可少,否则参数会被作为命令,例如没有 -- 时执行 pnpm test2 a,结果为:

> t1@1.0.0 test2 /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all "test:foo {1}" "test:foo {1} {2}" "test:foo {@}" "test:foo {*}" "a"ERROR: Task not found: "a"ELIFECYCLE  Command failed with exit code 1.

可以看到 a 被当做了一个任务。

加上 -- 后,执行 pnpm test2 1 aaa 1xy 的结果为:

> t1@1.0.0 test2 /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> npm-run-all "test:foo {1}" "test:foo {1} {2}" "test:foo {@}" "test:foo {*}" -- "1" "aaa" "1xy"> t1@1.0.0 test:foo /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./foo.sh "1"foo: [1]=1, [2]=, [3]=, [4]=, [5]=, [6]=> t1@1.0.0 test:foo /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./foo.sh "1" "aaa"foo: [1]=1, [2]=aaa, [3]=, [4]=, [5]=, [6]=> t1@1.0.0 test:foo /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./foo.sh "1" "aaa" "1xy"foo: [1]=1, [2]=aaa, [3]=1xy, [4]=, [5]=, [6]=> t1@1.0.0 test:foo /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> ./foo.sh "1 aaa 1xy"foo: [1]=1 aaa 1xy, [2]=, [3]=, [4]=, [5]=, [6]=

在例子中可以很清楚的看出不同占位符的区别:

  • {1}, {2}, … : 表示一个参数,{1} 表示第一个参数,{2} 表示第二个参数;
  • {@}: 表示所有参数;
  • {*}: 将所有参数合并为一个参数;

run-s / run-p

run-snpm-run-all -snpm-run-all --seriesnpm-run-all --sequential 的简写,表示串行执行命令。

run-pnpm-run-all -pnpm-run-all --parallel 的简写,表示并行执行命令。

scripts 中添加命令 "build-s": "run-s --silent build:*",执行结果为:

> t1@1.0.0 build-s /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> run-s --silent build:*2024年10月14日 星期一 21时24分26秒 CST: start build:css
2024年10月14日 星期一 21时24分27秒 CST: build:css finished!
2024年10月14日 星期一 21时24分27秒 CST: start build:html
2024年10月14日 星期一 21时24分28秒 CST: build:html finished!
2024年10月14日 星期一 21时24分29秒 CST: start build:js
2024年10月14日 星期一 21时24分32秒 CST: build:js finished!

可以看到3个build命令是串行的。这里使用了 --silent 参数,减少系统的日志输出。

添加命令 "build-p": "run-p --silent build:*",执行结果为:

> t1@1.0.0 build-p /Users/foolishflyfox/Code/Year2024/Mon10/day14/t1
> run-p --silent build:*2024年10月14日 星期一 21时26分48秒 CST: start build:html
2024年10月14日 星期一 21时26分48秒 CST: start build:js
2024年10月14日 星期一 21时26分48秒 CST: start build:css
2024年10月14日 星期一 21时26分49秒 CST: build:html finished!
2024年10月14日 星期一 21时26分49秒 CST: build:css finished!
2024年10月14日 星期一 21时26分51秒 CST: build:js finished!

可以看到3个命令是并行执行的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/882104.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OpenCV高级图形用户界面(17)设置一个已经创建的滚动条的最小值函数setTrackbarMin()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C11 算法描述 cv::setTrackbarMin 这个函数的作用就是设置指定窗口中轨迹条的最小位置。这使得开发者能够在程序运行时动态地调整轨迹条的范围,而不…

Leetcode—1242. 多线程网页爬虫【中等】Plus(多线程)

2024每日刷题&#xff08;187&#xff09; Leetcode—1242. 多线程网页爬虫 实现代码 /*** // This is the HtmlParsers API interface.* // You should not implement it, or speculate about its implementation* class HtmlParser {* public:* vector<string>…

Go程序的一生——Go如何跑起来的?

引入编译链接概述 编译过程 词法分析语法分析语义分析中间代码生成目标代码生成与优化链接过程Go 程序启动GoRoot 和 GoPathGo 命令详解 go buildgo installgo run总结参考资料 引入 我们从一个 Hello World 的例子开始&#xff1a; package mainimport "fmt"func…

PROFINET开发EtherNet/IP开发Vline板卡在称重设备行业的应用

本次分享的&#xff0c;是我们VlinePROFINET开发EtherNet/IP开发嵌入式板卡在称重行业的典型应用。 应用背景 在现代科技高度发达的时代&#xff0c;无论是科学研究、医疗诊断、制药生产还是工业制造&#xff0c;准确的测量和称重都是保证质量和效率的关键。 随着新项目实施…

【BGA布局布线-熬夜加班整理】

BGA CHIP PLACEMENT AND ROUTING RULE BGA 是 PCB 上常用的组件&#xff0c;通常 CPU、NORTH BRIDGE、SOUTH BRIDGE、 AGP CHIP、CARD BUS CHIP…等&#xff0c;大多是以 bga 的型式包装&#xff0c;简言之&#xff0c;80&#xfe6a;的 高频信号及特殊信号将会由这类型的 pac…

自动化测试与敏捷开发的重要性

敏捷开发与自动化测试是现代软件开发中两个至关重要的实践&#xff0c;它们相互补充&#xff0c;共同促进了软件质量和开发效率的提升。 敏捷开发的重要性 敏捷开发是一种以人为核心、迭代、循序渐进的软件开发方法。它强调以下几个核心价值观和原则&#xff1a; 个体和交互…

服务器技术研究分析:存储从HBM到CXL

服务器变革&#xff1a;存储从HBM到CXL 在《从云到端&#xff0c;AI产业的新范式&#xff08;2024&#xff09;》中揭示&#xff0c;传统服务器价格低至1万美金&#xff0c;而配备8张H100算力卡的DGX H100AI服务器价值高达40万美金&#xff08;约300万人民币&#xff09;。 从供…

jmeter使用文档

文章目录 一、安装使用1、下载2、bin/jmeter.properties介绍 二、windows使用1、微调&#xff08;1&#xff09;界面样式&#xff08;2&#xff09;修改语言 2、简单使用3、各组件详解&#xff08;1&#xff09;CSV 数据文件配置&#xff08;2&#xff09;BeanShell取样器 三、…

Golang | Leetcode Golang题解之第478题在圆内随机生成点

题目&#xff1a; 题解&#xff1a; type Solution struct {radius, xCenter, yCenter float64 }func Constructor(radius, xCenter, yCenter float64) Solution {return Solution{radius, xCenter, yCenter} }func (s *Solution) RandPoint() []float64 {r : math.Sqrt(rand.…

C++之设计原则

在C中&#xff0c;设计原则是一套指导软件开发过程中决策和设计模式的准则&#xff0c;旨在提高软件的可维护性、可扩展性、灵活性和可靠性。 以下是几种核心设计原则&#xff1a; 1.单一职责 功能单一&#xff0c;方便组合和复用。 图示&#xff1a; 应用场景&#xff1a;…

【godot游戏引擎学习笔记】初识界面

个人笔记&#xff0c;学习自B站视频BV1ut42177r8 目录 渲染器的选择 Forward 移动 兼容 编辑器页面 浏览场景&#xff08;左上角&#xff09; 文件浏览器&#xff08;左下角&#xff09; 属性检查器&#xff08;右侧&#xff09; 场景编辑器&#xff08;中间&#x…

electron本地OCR实现

使用tesseract.js - npm (npmjs.com) 官方demo&#xff1a;GitHub - Balearica/tesseract.js-electron: An example to use tesseract.js in electron 目录结构&#xff1a; // 引入 <script type"module" src"./ocr/tesseract.js"></script>…

如何设置 GitLab 密码过期时间?

GitLab 是一个全球知名的一体化 DevOps 平台&#xff0c;很多人都通过私有化部署 GitLab 来进行源代码托管。极狐GitLab 是 GitLab 在中国的发行版&#xff0c;专门为中国程序员服务。可以一键式部署极狐GitLab。 学习极狐GitLab 的相关资料&#xff1a; 极狐GitLab 60天专业…

Go-知识反射

Go-知识反射 1. 接口1.1 类型1.2 interface 类型1.2.1 interface 变量1.2.2 实现接口1.2.3 复合类型1.2.4 空 interface 2. 反射定律2.1 reflect 包2.2 反射可以将 interface 类型变量转换为反射对象2.3 反射可以将反射对象还原成 interface 对象2.4 反射对象可修改&#xff0c…

Oracle权限安全管理

实验内容 本次实验先使用system用户连接 温馨提示&#xff1a;题目要求切换账户登录的时候自己记得切换&#xff0c;本文章只提供相应的SQL语句 在表空间BOOKTBS1&#xff08;实验4第1题已创建&#xff09;创建一张表BOOKS,其字段如下&#xff1a;&#xff1a; SQL> create…

一次性语音芯片:重塑语音识别技术,引领智能化生活新时代

随着一次性语音芯片的突破性进展&#xff0c;语音识别技术正融入我们生活的方方面面&#xff0c;引领着智能化生活迈向一个全新的时代。这些芯片不仅体积小巧、成本低廉&#xff0c;更在性能上实现了质的飞跃&#xff0c;能够更精确地捕捉并理解人类语音。本文将解读关于一次性…

嵌入式开发选硬件还是软件?

做了单片机软件开发很多年&#xff0c;和很多硬件工程师打过配合&#xff0c;其中大部分硬件工程师&#xff0c;越往后&#xff0c;学软件的意愿越强烈。 所以我建议是&#xff0c;先做软件&#xff0c;是比较好的发育路线。 软件开发的优势主要体现在几个方面。 第一是薪资待遇…

python爬虫实战案例——从移动端接口抓取微博评论,采用cookie登陆,数据存入excel表格,超详细(15)

文章目录 1、任务目标2、网页分析3、代码编写3.1 代码分析3.2 完整代码1、任务目标 1、目标网站:微博文章(https://m.weibo.cn/detail/4813628149072458),这是微博某一篇博文,用于本文测试 2、要求:爬取该博文下,所有一级评论和二级评论,以及每条评论的作者,最后保存至E…

Python魔法函数__iter__的用法

下面是找到的一个比较好的科学解释&#xff1a; Python中可迭代对象(Iterable)并不是指某种具体的数据类型&#xff0c;它是指存储了元素的一个容器对象&#xff0c;且容器中的元素可以通过__iter__( )方法或__getitem__( )方法访问。 1.__iter__方法的作用是让对象可以用for…

Vscode替换\n为换行符

文件内容是字符串&#xff0c;每行用\n分割&#xff0c;阅读困难&#xff0c;可以在vscode&#xff0c;用替换\n进行换行。