[Chicago-talk] Using Storable to exchange data between 2 processes

Greg Fast gdf at speakeasy.net
Fri Jan 23 10:07:37 CST 2004


On Thu, 22 Jan 2004 22:51:08 -0600, "Jay Strauss" <me at heyjay.com> wrote:
> Hey that IPC::ShareLite is pretty neat.  Works for me, screw the people on
> windows, they can write to a file

Keen :)

Your code snippet looked pretty workable to me, though you might run
into problems is the write process and the read process are trying to
access the file simultaneously.

Oh, one more possibility (at the risk of jumping the gun on next
month's meeting topic): you could use POE, letting its cooperative
task-swapping handle what you're doing with multiple processes.
Depends on how responsive you require the read task to be, probably.

    use POE;
    use POE::Session;
    
    my $Data = {};
    
    my $task_one = POE::Session->create(
    	inline_states => { _start => sub { $_[KERNEL]->yield('task_one') },
                           task_one => \&task_one,
                           _stop => sub { "cleanup, as required" },
                         } );
    
    my $task_two = POE::Session->create(
    	inline_states => { _start => sub { $_[KERNEL]->yield('task_two') },
                           task_two => \&task_two,
                           _stop => sub { "ditto" },
                         } );
    
    $poe_kernel->run();
    
    sub task_one {
      my $kernel = $_[KERNEL];
      print scalar(localtime), ": Updating shared data...\n";
      $Data->{X}++;
      $kernel->delay_set( 'task_one', 60 ); # run again in 60 seconds
    }
    
    sub task_two {
      my $kernel = $_[KERNEL];
      print scalar(localtime), ": $Data->{X}\n";
      $kernel->delay_set( 'task_two', 35 );
    }


--
Greg Fast
http://cken.chi.groogroo.com/~gdf/



More information about the Chicago-talk mailing list