Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^5: Threading with a twist

by Random_Walk (Prior)
on Oct 04, 2017 at 07:23 UTC ( [id://1200645]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Threading with a twist
in thread Threading with a twist

We could reduce your question to: Why does using subroutines make code simple?

We are looking at two very different functions. It is logical to split them and keep two simple easy to debug blocks, rather than interleave them in a complex routine. Depending on the simplicity of the records going to <STDOUT> it may even be better to have the workers print directly. If the records are long and will not print as an atomic operation, or if there is post processing, then an publishing thread that gathers them and prints them keeps this work in one place.

Something like this looks very simple to me.

# some initialisation, queue building etc... my $shouty = threads->create('publish'); # This is the boss loop, dispatches to workers while (<>) { chomp; # I guess $work->enqueue $_; ) sub publish { while (my $out = $result->dequeue) { print "I got: $out\n"; }

Do you have an example where you read <STDIN> and the $result queue in the same code block, that is simpler?

Now if we need to add pre or post processing then it sits nicely in the dispatcher or publisher routine without being all mixed up.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1200645]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found