PERL POE ReadWrite Wheel File IO Example?

391 Views Asked by At

I'm having trouble finding an example of how to create and write to a file using a POE Wheel or whatever async process. I want to be able to write large files in a non blocking way. I'm looking for examples, but don't know where to start.

1

There are 1 best solutions below

0
On

So, I didn't really find a straight forward example for file io. But, I managed to figure out this working code from the examples I did find. I think it's what I want. I'm adding it here so if anyone has any comments it might help me or other improve it. Or, if someone else is looking for an example they can see this one.

                $self->{FILEIO_SESSION} = POE::Session->create(
                inline_states => {
                    _start => sub {
                        my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
                        $kernel->alias_set($fileio_uuid);

                        $heap->{wheels}->{$fileio_uuid} = POE::Wheel::ReadWrite->new(
                            InputHandle => $infile_fh ,
                            OutputHandle => $outfile_fh,
                            Driver => POE::Driver::SysRW->new(),
                            Filter => POE::Filter::Line->new(),
                            InputEvent => 'readLineEvent',
                            ErrorEvent => 'errorEvent'
                        );
                    },
                    _stop => sub {
                        my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
                        print "Removing fileio session\n";
                        $kernel->alias_remove($fileio_uuid);
                    },
                    readLineEvent => sub {
                        my ( $kernel, $heap, $input ) = @_[ KERNEL, HEAP, ARG0 ];
                        print "read a line...writing to file...\n";
                        $heap->{wheels}->{$fileio_uuid}->put($input);
                    },
                    errorEvent => sub {
                        my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
                        print "Everything either went to hell or we got to the end.  Shutting down...\n";
                        delete $heap->{wheels}->{$fileio_uuid};
                        $kernel->yield("_stop");
                    }
                }
            );