Frank Bax wrote:
I have script that takes a very long time to run - hours, sometimes even
days (even on a P2-2.8Ghz machine. After loading some data from a
database at the beginning (less than a second), the script does no i/o
until results are output at the end of script. I'd like to know how the
script is progressing through its data, so I added some code to update
the database at regular data intervals, but this has some problems:
- database is remote, so script goes much slower with these status
updates.
- updates are based on data instead of clock.
I read something about threads in perl and was wondering if these status
updates should be coded inside a thread so they have less impact on
overall script performance. The number crunching could still go on
while database update happens in separate thread.
Well, threads don't magically turn one CPU into two. However, this
particular case sounds like a good application for threads. Your main
crunching thread can be running along while the database updating thread
is blocked waiting for the database to respond.
To get updates based on clock instead of data... Are there tools within
perl for using clock/timer information? Do I have to parse clock/timer
info myself to make something happen every hour inside an existing loop?
You can just use the simple built-in time() function, which returns
seconds since the epoch. If you want to update the database once per
hour, you could do something like this:
my $t = time;
while (1) {
do_database_update();
$secs = time - ($t + 3600); # start of next update
sleep $secs if $secs > 0;
}
The tricky part is sharing data between the threads, which is extremely
sucky in Perl's ithreads implementation, IMO. Read perlthrtut for an
overview of the issues.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>