【发布时间】:2013-10-30 14:39:38
【问题描述】:
在 Mojolicious 应用程序中,我尝试在单击链接时将 ODT 文件转换为 HTML。我使用 shell 命令“soffice”转换文件。转换文件需要一些时间。我向用户发送状态消息以通知他进度。我通过写入 Mojo::Log 对象来发送这些状态更新消息。然后,我在 EventSource 路由中订阅此日志对象。
然后我遍历文件并使用 AnyEvent::Util run_cmd 执行外部“soffice”程序。
for my $file (@{ $filelist }) {
my $output_dir = './output_dir';
my $cmd = "soffice --headless --convert-to html --outdir '$output_dir' '$file'";
my $cv = AnyEvent->condvar;
my $w;
$w = run_cmd($cmd,
'>' => sub { my $out = shift;
&WriteToLog({ status => "cmd output '$out'..." });
undef $w;
$cv->send;
},
'2>' => sub { my $err = shift;
&WriteToLog({ status => "ERROR '$err'..." });
undef $w;
$cv->send;
}
);
$cv->recv;
}
从主要的 AnyEvent 教程中复制和粘贴了很多内容。如果只有很少的文件要转换(大约 2 或 3 个),那么一切顺利。通过 EventSource 连接发送的状态消息出现在客户端浏览器上。然后在所有文件都转换完成后,呈现网页。
如果要处理更多文件,则转换几个文件,然后出现线程标题中的错误消息。
包含上述代码的路由的路由是这样的:
my $initdocs = $r->under->to('docroute#initdocs');
$initdocs->get('/showdocs')->to('docroute#showdocs');
上面的代码在“initdocs”路径中。
感谢任何帮助。提前致谢。
【问题讨论】:
-
仅供参考,以后添加perl 标签将帮助更多人看到您的有趣问题。
标签: perl asynchronous mojolicious anyevent