Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Handling asynchronous events with Perl

by BrowserUk (Patriarch)
on Jul 11, 2005 at 00:14 UTC ( #473808=note: print w/replies, xml ) Need Help??

in reply to Handling asynchronous events with Perl

Your program could be as simple as:

#! perl -slw use strict; use threads; my $FILEPATH = './AUV*.dat'; my $file; while( 1 ) { sleep( 1 ) until defined( $file = glob $FILEPATH ); async{ process( $file ) }->detach; } sub process { my $file = shift; return unless $file =~ m[AUV(\d+)\.dat$]; my $AUVNO = $1; eval "use AUV::$AUVNO"; warn "No processing module found for AUV no: $AUVNO" and return if + $@; AUVProcess( $file ); return; }

This assumes that the inbound files are named AUVnnn.dat, with nnn representing each AUV.

You then create a separate package to process each AUV's data, that exports a single entrypoint that takes the filename to process as it's only arguement. The modules would have a consistant naming AUV::nnn, and would look something like this:

package AUV::123; use strict; our @ISA = 'Exporter'; our @EXPORT = 'AUVProcess'; sub AUVProcess{ my $file = shift; open DATA, '<', $file or warn "Couldn't open $file : $!\n"; ## Process data from file here print while <DATA>; close DATA; unlink $file; return; } print "AUV::123 loaded";

All this does it print the data to the terminal for testing.

This allows you to add and modify the processing modules without stopping the main dispather. The dispatcher will use negligable cpu while no files need processing. A file will be dispatched for processing within 1 second of it arrival. If a file arrives and it's module is unavailable, it will simply wait until the module becomes available, at which point it will be processed. The dispatcher should easily run unattended for mpnths without problems as the dispatcher uses minimal memory or processor and no memory growth. All the memory used to process the data is immediately freed back to the runtime for reuse when the thread terminates.

Add error checking, logging etc. as required.

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2023-03-24 13:31 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (61 votes). Check out past polls.