Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^2: Perl Threads Boss/Worker Example

by sunkid (Initiate)
on Jul 11, 2013 at 02:47 UTC ( #1043620=note: print w/replies, xml ) Need Help??

in reply to Re: Perl Threads Boss/Worker Example
in thread Perl Threads Boss/Worker Example

I realize that this is an old thread but I ran across this trying to solve a similar problem: run a specific number of threads to work on a set of issues. I thought it might be useful for others to know the correct implementation of Tanktalus' suggestion.

Basically, there were two issues that took me a while to sort out: firstly, the newly created threads are detached, which makes them impossible to join later; secondly (much less important), adding undef as a queue item isn't necessary when using dequeue_nb instead of dequeue.

The code that actually works for me looks like this:

#! /usr/bin/perl use threads; use Thread::Queue; 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())) { doStuff($item); } }); } 1..$thread_limit; # terminate. $_->join() for @thr;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043620]
[Discipulus]: Corion are you would able to realize such thing? O_O
[Corion]: In the same vein I have a script that automates Firefox to enter some data into another system. It's not faster than the people using the script if they were to do it manually, but they prefer not having to check the data and not having typos when ...
[Corion]: ... entering the data
[Corion]: Discipulus: I don't know whether I could really do that, but the init process itself mostly launches other processes, and the whole startup is just following a path of dependencies and making sure they are all running. Which basically is what ...
[Discipulus]: when at work my time is (temporarly) owned by the firm, so i do not care (coworkers whatch movies.. I code Perl)
[Corion]: ... make already does, except for files instead of programs. But maybe with some /proc hackery, that could be eliminated and one could use plain make :-D
[choroba]: systemd just makes is asynchronous
[choroba]: so, make -j
[Corion]: Discipulus: Yeah - but when writing Perl to save time (instead of having fun), it helps to look whether you're actually saving time ;) Why spend 5 minutes doing manually what you can spend three years automating? ;)
[Corion]: choroba: Oh, yeah :-D

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2017-07-27 09:30 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (408 votes). Check out past polls.