in reply to Re^2: perl script and queue
in thread perl script and queue
Hello 1nickt,
You are right, the OP never stated that he is using MySQL so I will update the answer.
Regarding Using a queue for this situation is quite sensible, in theory. Can you explain how to implement one with Thread::Queue? I would approach it like this. Example not real time code:
I use forks instead of threads because I get the error:
$ perl test.pl 1 2 This Perl not built to support threads Compilation failed in require at test.pl line 3. BEGIN failed--compilation aborted at test.pl line 3.
Having said that, sample of executable code:
#! /usr/bin/perl use forks; use strict; use warnings; use Thread::Queue; use feature 'say'; sub insert_to_db { say $_[0]; } my $q = Thread::Queue->new(); # A new empty queue # Send work to the threads $q->enqueue($_) for @ARGV; # Worker threads my $thread_limit = 8; my @thr = map { threads->create(sub { while (defined (my $item = $q->dequeue_nb())) { insert_to_db($item); } }); } 1..$thread_limit; # terminate. $_->join() for @thr; __END__ $ perl test.pl 1 2 Argument "2.56_01" isn't numeric in numeric ge (>=) at /home/user/perl +5/lib/perl5/x86_64-linux/forks.pm line 1570. 1 2
Of course the sample of code, in reality instead of ARG I would queue the number of insert statements and execute them in sequence.
Any way, my approach would be to queue the insert statements in order to complete each insert before the next one.
Hope this clears things more, BR.
|
|---|