1

我想要一个 gui 来显示我的变量的值,所以我可以看到它工作。下面的代码增加了 $counter 但标签不会像“textvaraible”那样更新。问题可能是作用域,线程在变量 $counter 的单独副本上工作,当您结束脚本时,INT_handler 打印 5,显示原始变量未更改。

#!/usr/bin/perl
use threads; use threads::shared; use warnings;

my $counter :shared = 5;
$counter_t = threads->create(\&counter); # counting thread
use Tk;
print "gui thread started\n";
my $mw = MainWindow->new;
$mw->geometry("100x100");
$label = $mw->Label(-textvariable => \$counter)->pack(qw/-anchor nw -padx 10/);

$SIG{'INT'} = 'INT_handler';

sub counter{    
    print "counter thread started\n";
    while(1){       
        sleep(1);
        $counter++;
        print $counter . "\n";
    }
}

MainLoop;

sub INT_handler {
    print "\nCounter value is " . $counter . "\n";
    exit(0);
}

那么,解决方案是什么?某种工会?共享变量似乎没有帮助,或者我没有正确使用它。使用“我们的”或“我的”作为计数器并没有什么不同

4

2 回答 2

1

一种方法是为此使用 Thread::Queue ——所有线程将推送(入队)信息到结果队列,您将在主代码中处理它:

sub start {

    my @result;

    $queue= Thread::Queue->new;
    $queue_processed = Thread::Queue->new;

    my @domains = get_domains($domains_filename);

    $queue->enqueue(@domains);

    my @threads= map { threads->create( sub { create_thread($_) } ) } ( 1 .. $CONFIG{NUMBER_OF_THREADS} );

    $_->detach for (@threads);

    my $counter = 0;

    while ( $counter < scalar @domains ) {

    my $result = $queue_processed->dequeue_nb;

    if ($result) {

        if ( $result->{status} ) {

        $txt_processed_domains->configure(-state => "normal");
        $txt_processed_domains->insert_end( $result->{domain} . ".com" . " => " . "Available!" );
        $txt_processed_domains->see("end");
        $txt_processed_domains->configure(-state => "disabled");
        Tkx::update();
        $counter++;

        Win32::Sound::Volume('100%');
        Win32::Sound::Play( $CONFIG{SOUND_FILE} );
        Win32::Sound::Stop();

        my $response = Tkx::tk___messageBox( -type => "yesno", -message => $result->{domain} . ".com" . " is " . "Available! Continue?", -icon => "question", -title => "Domain found" );

        unless ( $response eq 'yes' ) {

            exit;
        }
        }
        else {

        $txt_processed_domains->configure(-state => "normal");
        $txt_processed_domains->insert_end( $result->{domain} . ".com" . " => " . "Already taken!" );
        $txt_processed_domains->itemconfigure( $counter, -background => "#f0f0ff" );
        $txt_processed_domains->see("end");
        $txt_processed_domains->configure(-state => "disabled");
        Tkx::update();
        $counter++;
        }
    }

    }

    Tkx::tk___messageBox( -message => "Completed!" );
}

sub create_thread {

    my $thread_id = shift;

    my ($domain);

    while( $domain = $queue->dequeue_nb ) {

    my $mech = MyMech->new( autocheck => 1 );
    $mech->quiet(0);

    $mech->get( $CONFIG{BASE_URL} . "domains/search.aspx?domainToCheck=$domain&tld=..com" );

    if ( $mech->content() =~ m{is\s+available!}is ) {

        open my $fh, ">>", $result_filename or die "Couldn't create result file! $!";

        #$queue_processed->enqueue( "$domain.com => Available!" );
        $queue_processed->enqueue( { status => 1, domain => $domain, } );

        print $fh "$domain.com\n";
        close $fh;
    }
    else {

        $queue_processed->enqueue( { status => 0, domain => $domain, } );
    }

    #sleep $CONFIG{DELAY_BETWEEN_REQUESTS};
    }

    return 1;
}
于 2012-08-20T13:48:09.897 回答
1

http://www.perlmonks.org/?node_id=585533

回答了我的问题

#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;

# for shared vars .....
# declare, share then assign value
my $ret;
share $ret;
$ret = 0;
my $val = 0;
#create thread before any tk code is called
my $thr = threads->create( \&worker );

gui();    

# tk code only in main
sub gui {
    use Tk;
    my $mw = MainWindow->new();
    my $label = $mw->Label(
         -textvariable => \$val )->pack();

    $mw->repeat(10,sub{
              $val = $ret;              
             });
    MainLoop;
}

# no Tk code in thread
sub worker {
   for(1..10){
     print "$_\n"; 
     $ret = $_;
     sleep 1; 
    }
   $ret = 'thread done, ready to join';
   print "$ret\n";
}

分配发生在 gui sub 中,以便 -textvariable 正常工作

于 2012-08-22T10:00:15.603 回答