The following is duelafn's demonstration converted to using MCE::Hobo and MCE::Shared and runs in Perl lacking threads support. Locking is handled automatically behind the scene. I updated the code to run at each interval period.

Update: Use monotonic clock.

#!/usr/bin/perl -w use strict; use warnings; use MCE::Hobo; use MCE::Shared; use Time::HiRes qw(time sleep); # Use monotonic clock if available. use constant CLOCK_MONOTONIC => eval { Time::HiRes::clock_gettime( Time::HiRes::CLOCK_MONOTONIC() ); 1; }; sub _time { ( CLOCK_MONOTONIC ) ? Time::HiRes::clock_gettime( Time::HiRes::CLOCK_MONOTONIC() ) : Time::HiRes::time(); } my $DEBUG = 1; my $INTERVAL = $DEBUG ? 3.0 : 60.0; my $PRICING = MCE::Shared->scalar(0); my $RUNNING = MCE::Shared->scalar(1); sub price_thread { my $next_interval = _time() + $INTERVAL; while ($RUNNING->get()) { printf("# TIME %.3f\n", _time()) if $DEBUG; # Local variable to limit lock time. # Catch errors in eval my $pricing = eval { # Net::Curl to remote URL... request here. my $val = 1000 * rand(); # [from net::curl] # ... more? # last line will go to $pricing and $pricing gets undef on + exception $val }; $PRICING->set($pricing); # Pause between requests: my $time = _time(); if ($time > $next_interval) { # Wait till next interval if curl time is greater than $IN +TERVAL. $next_interval += $INTERVAL while $next_interval < $time; } sleep $next_interval - $time; $next_interval += $INTERVAL; } } sub main_loop { # Local variable to limit lock time and prevent changes mid-comput +ation my $pricing = 0; while ($RUNNING->get()) { $pricing = $PRICING->get(); # Optional - I don't know what you are doing with pricing, you + might want # to use 0 or undef to signal a request error (i.e., out-of-da +te data). next if !defined($pricing) or $pricing == 0; # Do something with $pricing; print "$pricing\n"; sleep 1; # Work simulation # return if QUIT CONDITION; } } my $thr = MCE::Hobo->create({ void_context => 1 }, \&price_thread); eval { main_loop() }; $RUNNING->set(0); $thr->join();

In reply to Re^2: making a loop script with a remote URL call faster by marioroy
in thread making a loop script with a remote URL call faster by brandonm78

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.