如何让 Perl/Tk 进度条显示外部进程正在运行?

发布于 2024-08-01 19:54:15 字数 169 浏览 4 评论 0原文

我正在创建一个 Perl/TK GUI 代码,它将在内部调用一个单独的 exe。 进度条小部件将是其中正在执行的唯一指示,但问题是,当您运行代码时,进度条会冻结,因为它必须首先完成单独 exe 的执行,完成后,进度上的活动可以更新。

有没有更好的方法可以对单独的exe同时执行进度,从而实时执行代码?

I am creating a Perl/TK GUI code that will call a seperate exe inside. The progress bar widget will be the only indication that execution is happening inside it but problem is, as you run the code, the progress bar freezes because it has to finish first the execution of the seperate exe and after it's done, activity on the progress can be updated.

Is there a better way to have a simultenous implementation of the progress with respect to the seperate exe so as to have the real time execution of the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

述情 2024-08-08 19:54:15

你如何调用外部程序? 如果您阻塞了诸如 system() 之类的东西,请不要这样做。 在单独的进程中运行该程序,然后轮询以检查它是否正在运行。 只要它正在运行,您就会更新进度指示器。

How are you calling the external program? If you are blocking on something like system(), don't do that. Run the program in a separate process then poll to check if it is running. As long as it is running, you update your progress indicator.

故事↓在人 2024-08-08 19:54:15

您是否从其他进程获得进度输出? 如果是这样,您可以使用 open() 来运行你的子流程。

open(MYSUBPROC, '-|', "myprocess args") or die "could not execute!";
while (<MYSUBPROC>)
{
    #do something...
}
close(MYSUBPROC) || warn "subproc returned error $?";

您还应该查看 perlipc 部分perldoc.perl.org/perlipc.html#Using-open()-for-IPC" rel="nofollow noreferrer">使用 open() 进行 IPC,以及 安全管道打开

Do you get output for progress from the other process? if so, you can use open() to run your subprocess.

open(MYSUBPROC, '-|', "myprocess args") or die "could not execute!";
while (<MYSUBPROC>)
{
    #do something...
}
close(MYSUBPROC) || warn "subproc returned error $?";

You should also take a look at the perlipc sections on using open() for IPC, and Safe pipe opens

拥有 2024-08-08 19:54:15

我对 Perl/Tk 的了解不够,无法快速给出一个例子。

worker.pl

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

for (0 .. 10) {
    print 10 * $_, "\n";
    sleep 1;
}

boss.pl

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

open my $worker_h, '-|', 'worker.pl'
    or die "Cannot open pipe to worker: $!";

while ( my $status = <$worker_h> ) {
    chomp $status;
    my $ticks = 2 * ($status/10);
    print '=' x $ticks, "\r" x $ticks;
    # do other stuff;
}

close $worker_h
    or die "Error closing pipe to worker: $?";

I don't know enough Perl/Tk to whip up a quick example.

worker.pl

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

for (0 .. 10) {
    print 10 * $_, "\n";
    sleep 1;
}

boss.pl

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

open my $worker_h, '-|', 'worker.pl'
    or die "Cannot open pipe to worker: $!";

while ( my $status = <$worker_h> ) {
    chomp $status;
    my $ticks = 2 * ($status/10);
    print '=' x $ticks, "\r" x $ticks;
    # do other stuff;
}

close $worker_h
    or die "Error closing pipe to worker: $?";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文