简体   繁体   中英

Perl multithreading - thread doesn't start

I need some help, I can't figure out why my thread doesn't want to start. I don't have experience with perl, and was asked to make a script that will process a file row by row. Depending on the row, the process should execute other functions (not in snippet), call the same function on a new file or call the same function on a new file in parallel (thread).

Below, I pasted a snippet of the actual code (removed the non-relevant code).

I'm testing the multithreading part on a function called "test" which should print "ok".

The process executes correctly, "start" is printed, but then it gets stuck and after a brief delay, the process stops executing completely.

A thousand thanks to whoever may help me!

use strict;
use warnings;
use IO::Prompter;
use Getopt::Long;
use Log::Message::Simple;
use File::Basename;
use File::Spec;
use IO::Socket::INET;
use UUID::Tiny ':std';
use threads;
use threads::shared;

# *bunch of code deleted*

process_file( $cmdline{csvfile}, 1 );

sub test {
    print "ok\n";
}

sub process_file {

    # get parameters
    my ( $input_file, $flowid ) = @_;

    # init variables

    # open input file
    open( my $fh, '<:encoding(UTF-8)', $input_folder . $input_file )
        or die "Could not open file '$input_file' $!";

    # process file
    while ( my $row = <$fh> ) {
        chomp $row;
        @request   = split ";", $row;
        $flow_type = $request[0];
        $flow      = $request[1];

        # *bunch of code deleted*

        $filename    = "$flow.csv";
        $keep_flowid = $request[2];                       # keep flowid?
        $tmp_flowid  = $keep_flowid ? $flowid : undef;    # set flowid
        $thread      = $request[3];
        if ( $thread == 1 ) {
            ### Create new thread
            print "start\n";
            my $process_thread = threads->create("test");
            $process_thread->join();
        }
        elsif ( $thread == 0 ) {

            # wait on process to complete
            process_file( $filename, $tmp_flowid );
        }

        # *bunch of code deleted*

    }

    # close file
    close $fh or die "Couldn't close inputfile: $input_file";
}

It's hard to say exactly why you're having this problem - the major possiblity seems to be:

    $thread = $request[3];
    if ($thread == 1){

This is input from your filehandle, so a real possiblity is that "$request[3]" isn't actually 1 .

I am a bit suspicious though - your code as use strict; use warnings at the top, but you're not declaring eg $thread , $flow etc. with my . That either means you're not using strict , or you're reusing variables - which is a good way to end up with annoying glitches (like this one).

But as it stands - we can't tell you for sure, because we cannot reproduce the problem to test it. In order to do this, we would need some sample input and a MCVE

To expand on the point about threads made in the comments - you may see warnings that they are "Discouraged". The major reason for this, is because perl threads are not like threads in other languages. They aren't lightweight, where in other languages they are. They're perfectly viable solutions to particular classes of problems - specifically, the ones where you need parallelism with more IPC than a fork based concurrency model would give you.

I suspect you are experiencing this bug , fixed in Perl 5.24.

If so, you could work around it by performing your own decoding rather than using an encoding layer.

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