Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Threading with a twist

by Random_Walk (Prior)
on Oct 03, 2017 at 12:44 UTC ( #1200610=note: print w/replies, xml ) Need Help??


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

Because KISS

The design in the OP gives two very disparate jobs to the 'Boss'. It must flip between checking if there are results on the results queue and handling them, then seeing if there is anything to read on <STDIN> and passing it off to workers. The OP is talking about using non blocking calls and sleeps to do this. The complexity clearly recognised by the multiplicity of punctuation marks ... Monitor $RXQUEUE and <STDIN> - simultaneously???

If we split these two functions then we have two simple tasks.

  • Listen on <STDIN> and put work on worker queue.
  • Listen on results queue and publish results
both tasks can be written with blocking calls ensuring they sleep efficiently until they have work, then spring to work with alacrity when there is something to do.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^4: Threading with a twist
by Anonymous Monk on Oct 03, 2017 at 16:48 UTC
    Having extra thread only for print is kiss?

      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
Node Status?
node history
Node Type: note [id://1200610]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2021-04-22 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?