[26961] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8917 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 3 18:05:37 2006

Date: Fri, 3 Feb 2006 15:05:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 3 Feb 2006     Volume: 10 Number: 8917

Today's topics:
        Fast pipe communication (See Website For Email)
    Re: Fast pipe communication xhoster@gmail.com
        Threading and production environments <socyl@987jk.com.invalid>
    Re: Threading and production environments xhoster@gmail.com
    Re: XML::Simple Parsing with Attributes problem <john@heathdrive.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 03 Feb 2006 13:17:42 -0800
From: "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail@moderncppdesign.com>
Subject: Fast pipe communication
Message-Id: <Iu4r5I.12yt@beaver.cs.washington.edu>

Hello everybody,


I've written a small utility that allows Perl to communicate with TeX 
(knowledge of TeX is not necessary to understand my question). Basically 
I run perl to communicate with two pipes: perlin.tex and perlout.tex. 
Perl reads a line off perlin.tex, evaluates the string, and writes it to 
perlout.tex. Here's what I came up with:

===========================================
#!/bin/env perl
use strict;
use warnings;

system("rm -f perlin.tex perlout.tex && mkfifo perlin.tex perlout.tex") == 0
   or die "Couldn't create pipes";

while () {
   print "Opening perlin.tex...\n";
   open(my $perlin, "perlin.tex") or die "Can't open: $!";
   print "Opened perlin.tex\n";
   print "Opening perlout.tex...\n";
   open(my $perlout, ">perlout.tex") or die "Can't open: $!";
   print "Opened perlout.tex\n";
   $| = 1;
   while (<$perlin>) {
     chomp;
     print "Read a line: `$_'\n";
     next if $_ eq "";
     my $r = eval($_);
     die $@ if $@;
     chomp $r;
     print $perlout "$r\n";
   }
   print "Input pipe finished.\n";
}
===========================================

Test code:

$ perl perlengine.pl &
$ echo "1+1" > perlin.tex
$ cat perlout.tex
2
$ _

(Ignoring, of course, all of the debug messages.)

The question is, how can I make the code more efficient? I noticed that 
every evaluation is really a pass through the outer loop. In other 
words, I was unable to convince perl to block in <$perlin> once the 
process writing to perlin.tex has finished. I need to reopen perlin.tex 
for that; the call to open will block until someone wrote to perlin.tex.

Is there any chance of avoiding the expensive trip of opening and 
closing the whole thing over and over again? Can I tell perl, "read to 
the EOF of this pipe, and then rewind and read again, blocking if nobody 
wrote to it"?


Thanks,

Andrei



------------------------------

Date: 03 Feb 2006 22:41:08 GMT
From: xhoster@gmail.com
Subject: Re: Fast pipe communication
Message-Id: <20060203174256.766$G7@newsreader.com>

"Andrei Alexandrescu (See Website For Email)"
<SeeWebsiteForEmail@moderncppdesign.com> wrote:
> Hello everybody,
>
> I've written a small utility that allows Perl to communicate with TeX
> (knowledge of TeX is not necessary to understand my question). Basically
> I run perl to communicate with two pipes: perlin.tex and perlout.tex.
> Perl reads a line off perlin.tex, evaluates the string, and writes it to
> perlout.tex. Here's what I came up with:
>
> ===========================================
> #!/bin/env perl
> use strict;
> use warnings;
>
> system("rm -f perlin.tex perlout.tex && mkfifo perlin.tex perlout.tex")
> == 0
>    or die "Couldn't create pipes";
>
> while () {
>    print "Opening perlin.tex...\n";
>    open(my $perlin, "perlin.tex") or die "Can't open: $!";
>    print "Opened perlin.tex\n";
>    print "Opening perlout.tex...\n";
>    open(my $perlout, ">perlout.tex") or die "Can't open: $!";
>    print "Opened perlout.tex\n";
>    $| = 1;

This turns on autoflush only for STDOUT.  You probably need to turn it on
for $perlout as well (although that is not the cause of the current
problem.)

use IO::Handle; # This should go near the top of the script, not here
    $perlout->autoflush(1);

>    while (<$perlin>) {
>      chomp;
>      print "Read a line: `$_'\n";
>      next if $_ eq "";
>      my $r = eval($_);
>      die $@ if $@;
>      chomp $r;
>      print $perlout "$r\n";
>    }
>    print "Input pipe finished.\n";
> }
> ===========================================
>
> Test code:
>
> $ perl perlengine.pl &
> $ echo "1+1" > perlin.tex
> $ cat perlout.tex
> 2
> $ _
>
> (Ignoring, of course, all of the debug messages.)
>
> The question is, how can I make the code more efficient? I noticed that
> every evaluation is really a pass through the outer loop. In other
> words, I was unable to convince perl to block in <$perlin> once the
> process writing to perlin.tex has finished.

That's correct.  Perl will not block on handles which are closed on the
other end.

> I need to reopen perlin.tex
> for that; the call to open will block until someone wrote to perlin.tex.

So it blocks at a slightly different place.  Why is that a problem?

> Is there any chance of avoiding the expensive trip of opening and
> closing the whole thing over and over again?

You could move the opening of the perlout handle out of the loop, I see no
reason it needs to be repeated each time.

Anyway, assuming there were a way to hook a new process up to an existing
but closed pipe on one end, why should that be significantly more efficient
than re-opening the pipe?  Both operations seem to be of about the same
complexity. The way to improve efficiency is to make the writing process
stay on the line rather than hanging up every time.

> Can I tell perl, "read to
> the EOF of this pipe, and then rewind and read again, blocking if nobody
> wrote to it"?

I know of no way to do it.  It seems like that more a matter for the OS
than for Perl.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: Fri, 3 Feb 2006 19:05:27 +0000 (UTC)
From: kj <socyl@987jk.com.invalid>
Subject: Threading and production environments
Message-Id: <ds09hm$gcc$1@reader2.panix.com>




When I tried to rebuild DBI today, I got the following warning:

*** You are using a perl configured with threading enabled.
*** You should be aware that using multiple threads is
*** not recommended for production environments.

I fought hard the decision to compile perl with threading support
because I was aware of this recommendation, but I was overruled by
our sysadmin and several co-workers.

I am inclined to revisit the subject, but I'd like to know how
current the warning above really is, and whether the problems with
enabling threading affect programs that don't explicitly use
threading.

Your opinions on the subject would be very much appreciated.  The
OS is Linux (SuSE) and the architecture is i686.

Thanks,

kj

-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.


------------------------------

Date: 03 Feb 2006 23:00:36 GMT
From: xhoster@gmail.com
Subject: Re: Threading and production environments
Message-Id: <20060203180224.677$QU@newsreader.com>

kj <socyl@987jk.com.invalid> wrote:
> When I tried to rebuild DBI today, I got the following warning:
>
> *** You are using a perl configured with threading enabled.
> *** You should be aware that using multiple threads is
> *** not recommended for production environments.
>
> I fought hard the decision to compile perl with threading support
> because I was aware of this recommendation, but I was overruled by
> our sysadmin and several co-workers.
>
> I am inclined to revisit the subject, but I'd like to know how
> current the warning above really is, and whether the problems with
> enabling threading affect programs that don't explicitly use
> threading.

I have used DBI extensively (MySQL and Oracle) in non-threaded code running
on a threaded build of perl, and I have never encountered a problem.
(Well, I encouter all kinds of problems, but not ones related to the
threadedness. :) )

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: Fri, 3 Feb 2006 22:47:07 -0000
From: "John" <john@heathdrive.com>
Subject: Re: XML::Simple Parsing with Attributes problem
Message-Id: <OJudndMtKaX2QH7eRVny3g@eclipse.net.uk>

Many thanks.  I had forgoten about the Dumper.

I had an *array * of hashes.

Corrected line should be (in case anybody else reads the thread)  as 
follows:-

$key=$data->{england}->[0]->{town}->[$i]{code};
$value=$data->{england}->[0]->{town}->[$i]{contents};

for $i = 1 to num.

Regards
John



"Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote in 
message news:Xns975F8471CAEFDelhber1lidotechnet@10.232.40.227...
> "John" <john@heathdrive.com> wrote:
>
>> Hi - problem parsing with an attribute name.
>>
>> <england>
>> <town code="LON">Capital City</town>
>> <town code="MAN">Manchester</town>
>> <town code="BHAM">Birmingham</town>
>> </england>
>>
>> I'm using XML::Simple.
>>
>> $xml = new XML::Simple (ForceArray=>1, suppressempty=>1);
>> $data = $xml->XMLin($xmlfile);
>>
>> $which=$data->{england}->[0]->{town}->[0]; doesn't work.
>>
>> I need to access both  attribute value and its contents (BHAM and
>> Birmingham).
>>
>> Any ideas?
>
>
> Print out the structure XML::Simple creates (using Data::Dumper) and see 
> for yourself. I suspect it
> should be something along the lines of
>
>   $data->{'england'}->{'town'}->[0]->{'code'}
>
> to get the code of the first town.
>
>
> -- 
> Cheers,
> Bernard 




------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8917
***************************************


home help back first fref pref prev next nref lref last post