简体   繁体   中英

Perl: How to push a hash into an array that is outside of a subroutine

I originally experimented with trying to send a hash object through Thread::Queue, but according to this link , my versions of Thread::Queue and threads::shared is too old. Unfortunately, since the system I'm testing on isn't mine, I can't upgrade.

I then tried to use a common array to store my hashes. Here is the code so far:

#!/usr/bin/perl
use strict;
use warnings;

use threads;
use Thread::Queue;
use constant NUM_WORKERS => 10;

my @out_array;
test1();

sub test1
{
    my $in_queue = Thread::Queue->new();
    foreach (1..NUM_WORKERS) {
        async {
            while (my $job = $in_queue->dequeue()) {
                test2($job);
            }
        };
    }

    my @sentiments = ("Axe Murderer", "Mauler", "Babyface", "Dragon");

    $in_queue->enqueue(@sentiments);
    $in_queue->enqueue(undef) for 1..NUM_WORKERS;
    $_->join() for threads->list();

    foreach my $element (@out_array) {
        print "element: $element\n";
    }
}

sub test2
{
    my $string = $_[0];
    my %hash = (Skeleton => $string);
    push @out_array, \%hash;
}

However, at the end of the procedure, @out_array is always empty. If I remove the threading parts of the script, then @out_array is correctly populated. I suspect I'm implementing threading incorrectly here.

How would I correctly populate @out_array in this instance?

You need to make it shared

 use threads::shared;
 my @out_array :shared;

I don't think you need to lock it if all you do is push onto it, but if you did, you'd use

 lock @out_array;

You need to share any array or hash referenced by a value you push onto it using the tools in thread::shared.

 push @out_array, share(%hash);

Though as I mentioned earlier, I'd use a Thread::Queue.

sub test2 {
    my ($string) = @_;
    my %hash = ( Skeleton => $string );
    return \%hash;
}

...

my $response_q = Thread::Queue->new()
my $running :shared = NUM_WORKERS;

...

    async {
        while (my $job = $request_q->dequeue()) {
            $response_q->enqueue(test2($job));
        }

        { lock $running; $response_q->enqueue(undef) if !--$running; }
    };

...

$request_q->enqueue(@sentiments);
$request_q->enqueue(undef) for 1..NUM_WORKERS;

while (my $response = $response_q->dequeue()) {
    print "Skeleton: $response->{Skeleton}\n";
}

$_->join() for threads->list();

Note that lack of anything thread-specific in test2. This is good. You should always strive for separation of concerns.

You need to return your data from thread:

....
        async {
            my $data;
            while (my $job = $in_queue->dequeue()) {
                $data = test2($job);
            }

            return $data;
        };
...

for ( threads->list() ) {

    my $data = $_->join();
    #now you have this thread return value in $data
}

sub test2
{
    my $string = $_[0];
    my %hash = (Skeleton => $string);
    return \%hash;
}

I found my answer in the example here .

I had to change 2 things:

  • share the @out_array outside both subs
  • share the %hash in test2
  • add return; to the end of test2

Code outside both subs:

my @out_array : shared = ();

test2 sub:

sub test2
{
    my $string = $_[0];
    my %hash : shared;
    $hash{Skeleton} = $string;
    push @out_array, \%hash;
    return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM