perl - 从 Perl 脚本异步执行 Bash 命令

标签 perl bash asynchronous

我必须运行 Bash 命令。但是这个命令需要几分钟才能运行。

如果我正常(同步)执行此命令,我的应用程序将挂起,直到命令运行完毕。

如何从 Perl 脚本异步运行 Bash 命令?

最佳答案

你可以使用threads异步启动Bash,

use threads;
my $t = async {
  return scalar `.. long running command ..`;
};

然后手动测试线程是否准备好加入,并以非阻塞方式获取输出,

my $output = $t->is_joinable() && $t->join();

关于perl - 从 Perl 脚本异步执行 Bash 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16769378/

相关文章:

linux - 从多个 .tar.gz 文件中提取特定文件名以构建一个 .csv 文件

c# - 发送参数后异步等待事件

perl - 管道的 Ctrl+Z 字符和 EOF 问题

perl - "do something"的语法如果 "condition"

perl - 如何摆脱 Perl 的 GD::Graph 中的饼图轮廓?

bash - stdin 到 zip 命令而不是提示符

bash - 在bash脚本: how to signal to the (human) user that an error triggered a premature exit?中

linux - 如何让switch执行2个case

javascript - Ember : Resolve complex async promises

javascript - 无法在node js中使用 sleep 或暂停