Zig 中的 child process #58
jiacai2050
started this conversation in
观点交流
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
在工作中,难免会去重复执行某些任务,直到其成功为止。比如下载依赖,之前我的做法是用 Shell
这样虽然可以,但是有些麻烦,于是就想能不能实现个 cli,来替我们执行,如果用 C 实现的话,可以预想到的,就是 fork + exec,貌似也不是很麻烦。
但是如果要在主进程中,等待 child 完成之前,收集 child 的 stdout,而不堵塞主进程,那就要采用异步的 API 来轮训 stdout,比如 select
或 poll, Zig 里面有个 child_process.zig,它里面把 fork + exec + poll 都封装好了,直接使用即可,一些关键函数:
这是收集 stdout 的,创建进程的地方在
spawnPosix
中,感兴趣的可以自行查看,后来有机会再来介绍。最后,下面是我基于 Zig child process 实现的 repeat 命令:
推荐阅读
Beta Was this translation helpful? Give feedback.
All reactions