Perl并行运行两个while循环子例程

Mik*_*ikG 5 perl multithreading

我希望并行运行两个子程序,其中两个子程序使用ADB命令在Android手机上执行相同的任务.在SO和其他研究的帮助下,我在下面编写了以下代码,但是我是多线程新手,在执行过程中出现了"Free to wrong pool"错误.我假设我得到这个,因为我在两个线程中使用$ _变量,这是正确的吗?我使用Windows7来运行它,但我的Perl解释器在运行此脚本时崩溃.任何指导将不胜感激.谢谢.

use strict;
use Win32::OLE;
use EFS_Handle;
use HelperFunctions;
use threads;

#### Various ADB command sequences follow ####
#### Start of multithread to run the same function on different android handsets ####

my @jobs;

push @jobs, threads->create(
    sub {
        print "\n\t" . curTime() . " :\t DUT Time at start of MPLMN search";

        open my $fh1, '>', "output.txt" or die "Cannot open output.txt: $!";
        my $pid1 = open my $log1, "-|", "adb -s 42d8d7dd logcat";

        system('adb -s 42d8d7dd shell input keyevent KEYCODE_ENTER');

        while (<$log1>) {
            $fh1->print($_);
            last if m/Sorted scan results/;
        }
        kill "TERM", $pid1;
        close $log1;
        print "\n\t" . curTime() . " :\t DUT Time at End of MPLMN search\n";
    }
);

push @jobs, threads->create(
    sub {
        print "\n\t" . curTime() . " :\t REF Time at start of MPLMN search";

        open my $fh, '>', "output.txt" or die "Cannot open output.txt: $!";
        my $pid = open my $log, "-|", "adb -s 0123456789ABCDEF logcat";

        system('adb -s 0123456789ABCDEF shell input keyevent KEYCODE_ENTER');

        while (<$log>) {
            $fh->print($_);
            last if m/EVENT_NETWORK_SCAN_COMPLETED/;
        }
        kill "TERM", $pid;
        close $log;
        print "\n\t" . curTime() . " :\t REF Time at End of MPLMN search\n";

    }
);

$_->join for @jobs;
Run Code Online (Sandbox Code Playgroud)

小智 0

我认为问题可能与您使用“>”从两个线程写入同一文件“output.txt”这一事实有关。尝试用“>>”打开它们。

还要记住关闭该文件。