Glib::IO->add_watch - file descriptor got locked till the end of all incoming data

View: New views
4 Messages — Rating Filter:   Alert me  

Glib::IO->add_watch - file descriptor got locked till the end of all incoming data

by anguila :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I want to update a treeview and this is the reason why i'm using the add_watch function. The child is sending data all the time for many reasons and the problem that i have is the file handler which have the incoming data from child to parent. With watcher each time the child sent data the $line = <$reader> must save this data in $line and call the function.
 But dont know why  the <$reader> just get locked till the end of all $writer>write()  and then it save all those data into $line, and i dont want that, i want to save the data of ->write in $line each time child send data.

Any idea?

 my ($reader, $writer);
    $reader = FileHandle->new;
    $writer = FileHandle->new;

    socketpair($reader, $writer, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!";
    my $pid = fork();
    $reader->autoflush(1);
    $writer->autoflush(1);
    if ($pid){
        # PARENT
        shutdown($writer, 0);
        print "Pare!\n";
        my $line;
        $watcher{$pid} = Glib::IO->add_watch($reader->fileno(), ['in', 'hup'], sub {
                my ($fileno, $condition) = @_;

                print "->cond = $condition\n";
                if ($condition & 'in') {
                    print "->reading from parent!!!\n";
                    $line = <$reader>;
                    print "--->line = $line!\n";
                    my @data=split(/,/,$line);
                    &update_treeview(@data);
                }
                if (($condition & 'hup') and (! defined($line) or $line eq '')) {
                    return FALSE;  # uninstall
                }
                else {";
                    my $line = <$reader>;
                    print "\n---line= $line\n";
                    my @data=split(/,/,$line);
                    &update_treeview(@data);
                }
                return TRUE;  # continue without uninstalling
           });
    }
else {
   ...
   child sent info to parent through $writer->write(data)
 and then $writher->flush;

}


Thanks,


David

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Glib::IO->add_watch - file descriptor got locked till the end of all incoming data

by muppet-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Aug 2, 2009, at 3:03 AM, anguila wrote:

> I want to update a treeview and this is the reason why i'm using the  
> add_watch function. The child is sending data all the time for many  
> reasons and the problem that i have is the file handler which have  
> the incoming data from child to parent. With watcher each time the  
> child sent data the $line = <$reader> must save this data in $line  
> and call the function.
>  But dont know why  the <$reader> just get locked till the end of  
> all $writer>write()  and then it save all those data into $line, and  
> i dont want that, i want to save the data of ->write in $line each  
> time child send data.
>
> Any idea?

>         $watcher{$pid} = Glib::IO->add_watch($reader->fileno(),  
> ['in', 'hup'], sub {
>                 my ($fileno, $condition) = @_;
>
>                 print "->cond = $condition\n";
>                 if ($condition & 'in') {
>                     print "->reading from parent!!!\n";
>                     $line = <$reader>;

Don't mix and match buffered reads and an unbuffered event watcher.

The <> operator will read until end of line, but the watch is based on  
select(), and fires when any data is available on the file  
descriptor.  The end of line may not have arrived yet, and the <> will  
block until the end of line does arrive.

You want to use sysread() here.  This may involve keeping a partial  
string checking to see if it contains an entire line.



--
Teeter tots are shaped like marshmallows.
   -- Zella.

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Glib::IO->add_watch - file descriptor got locked till the end of all incoming data

by anguila :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Solved, I just put a new line to set the end of line and it works :), i cant use sysread because i doesnt know the length of the input each time.


On Sun, Aug 2, 2009 at 7:34 PM, muppet <scott@...> wrote:

On Aug 2, 2009, at 3:03 AM, anguila wrote:

I want to update a treeview and this is the reason why i'm using the add_watch function. The child is sending data all the time for many reasons and the problem that i have is the file handler which have the incoming data from child to parent. With watcher each time the child sent data the $line = <$reader> must save this data in $line and call the function.
 But dont know why  the <$reader> just get locked till the end of all $writer>write()  and then it save all those data into $line, and i dont want that, i want to save the data of ->write in $line each time child send data.

Any idea?

       $watcher{$pid} = Glib::IO->add_watch($reader->fileno(), ['in', 'hup'], sub {
               my ($fileno, $condition) = @_;

               print "->cond = $condition\n";
               if ($condition & 'in') {
                   print "->reading from parent!!!\n";
                   $line = <$reader>;

Don't mix and match buffered reads and an unbuffered event watcher.

The <> operator will read until end of line, but the watch is based on select(), and fires when any data is available on the file descriptor.  The end of line may not have arrived yet, and the <> will block until the end of line does arrive.

You want to use sysread() here.  This may involve keeping a partial string checking to see if it contains an entire line.



--
Teeter tots are shaped like marshmallows.
 -- Zella.



_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Glib::IO->add_watch - file descriptor got locked till the end of all incoming data

by muppet-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Aug 3, 2009, at 2:14 PM, anguila wrote:

> Solved, I just put a new line to set the end of line and it works :)

If you control both sides, that's the cleanest solution.  Rock.


> i cant use sysread because i doesnt know the length of the input  
> each time.

That's generally not a problem if the file handle is set to non-
blocking; sysread() should read what's available up to the buffer size  
you give.


--
The trees on the bus go "pyoo pyoo pyoo..."
   -- Yvonne, singing, um, something

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list