You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Tom Schindl <to...@gmx.at> on 2004/08/04 09:53:27 UTC
Re: request appears to be processed twice with PDF::Create
Hi,
what you really want is an intelligent caching behaviour:
1. Generate PDF if not exists in Filesystem (the advantage is that
multiple processes have a access to this PDF) or BerkleyDB for
example
2a. Sent Generatated PDF from Cache
2b. Regenerate PDF if underlying data has been modified and restore for
later
[3. A cronjob which cleans up your disk-cache from time to time]
Code looks like the following(Apache2):
-------------------------8<-------------------------
use Apache::RequestIO ();
use Apache::RequestRec ();
use Apache::Constants ();
sub handler {
my $r;
## ....
my $cache_key = md5( $user . $path );
my $file = "/tmp/pdf-cache/$cache_key.pdf";
my $rc;
if( -e $file && stat($_[0])[9] > stat($path)[9] ) {
eval {
$r->sendfile($file);
}
if( ! $@ ) {
return Apache::OK;
}
}
## create your PDF like you've done before
## ....
open( CACHEFILE, ">$file" );
print CACHEFILE $content;
close( CACHEFILE )
}
-------------------------8<-------------------------
Dermot Paikkos wrote:
> Hi Tom,
>
> I think I see what your saying. What if you ignored the first and
> worked on the 2nd? Same problem I guess if you hit the first child. I
> haven't tried to determine if there are two processes as a result of
> hitting the form but I know from the logs that the module starts
> cycling through the form data again.
>
> It is probably very sloppy but my problem was that given a username
> and their file, the file was being opened twice which made parsing a
> bit tricky. All I want is to open the file once. I am open to more
> elegant suggestions though :-)
> Dp.
>
>
> On 30 Jul 2004 at 2:41, Tom Schindl wrote:
>
>
>>Hi,
>>
>>Are you sure that's working as you expected? If I got you right I'm
>>afraid I have to disappoint you but this only works if the second
>>request is handled by the same apache-child as the first one and
>>there's another problem. If you come back one day later and hit the
>>same apache-child which already processed the first request, you won't
>>get anything.
>>
>>Tom
>>
>>Dermot Paikkos wrote:
>>
>>
>>>Arnaud,
>>>
>>>I have found a way around this. I don't know if your interested but
>>>it goes likes something like this:
>>>
>>> foreach my $param ($r->param) {
>>> if ($param =~ /\busers\b/) {
>>> $users{$r->param($param)} = 0;
>>> }
>>>....snip...then later
>>>
>>> foreach my $key (keys %users) {
>>> next if ($users{$key} == 1;
>>> $users{$key} = 1;
>>> }
>>>
>>>The idea being you only work request that haven't been processed yet.
>>>Once you process a request you increment that hash key to 1 and can
>>>avoid using it again. IE still sends the request twice and it is
>>>working with the first request not the second.
>>>
>>>Just a thought.
>>>Dp.
>>>
>>>
>>>
>>>On 29 Jul 2004 at 16:20, Arnaud Blancher wrote:
>>>
>>>
>>>
>>>
>>>
>>>>Dermot Paikkos a écrit :
>>>>
>>>>
>>>>
>>>>
>>>>>Does this mean you have to go an clean up these files later
>>>>>
>>>>>
>>>>>
>>>>
>>>>yes, if you dont want they stay on the disk.
>>>>
>>>>
>>>>
>>>>
>>>>>or is
>>>>>this done when the process ends?
>>>>>
>>>>>
>>>>>
>>>>
>>>>maybe you can write a special handle for the directory where you ll
>>>>write your pdf that delete the pdf when the connection (due to the
>>>>redirect) will be close by the client (but i'not sure).
>>>>
>>>>
>>>>
>>>>
>>>>>I don't want to slow the users down
>>>>>unless I have to.
>>>>>
>>>>>I think I would like to determine the user-agent and work around
>>>>>the repeating requests....somehow. Do you know how to find out the
>>>>>user- agent when using Apache::Request? I can't see it when I use
>>>>>this object. Thanx. Dp.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>~~
>>>Dermot Paikkos * dermot@sciencephoto.com
>>>Network Administrator @ Science Photo Library
>>>Phone: 0207 432 1100 * Fax: 0207 286 8668
>>>
>>>
>>>
>>>
>>
>>
>>--
>>Report problems: http://perl.apache.org/bugs/
>>Mail list info: http://perl.apache.org/maillist/modperl.html
>>List etiquette: http://perl.apache.org/maillist/email-etiquette.html
>>
>>
>
>
>
> ~~
> Dermot Paikkos * dermot@sciencephoto.com
> Network Administrator @ Science Photo Library
> Phone: 0207 432 1100 * Fax: 0207 286 8668
>
>
Reclaim Your Inbox!
http://www.mozilla.org/products/thunderbird
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: request appears to be processed twice with PDF::Create
Posted by Dermot Paikkos <de...@sciencephoto.com>.
Hi.
On 4 Aug 2004 at 14:20, Tom Schindl wrote:
> Oh. I see not a very heavy loaded service :-). Are you running the
> server on win32?
No slackware linux with Apache/1.3.26 (Unix) mod_perl/1.27
> > That besides, the above method didn't work (see mail to list
> > "Accessing form multiples". I think you replied to it. Whenever I
> > tried to loop through the incoming list of
> > "user=dermot+paikkos&user=joe+blogs" I was only getting the first
> > user.
>
> Although that's perl here's the explanation.
>
> because $r->param( "key" ) can be used in 2 contexts
> (see perldoc -f wantarray). That's something very specific to
> perl which you maybe not familiar with when have used langs
> like java, c++, ...:
Ha yes, well no, perl is the only language I have tried. I am a
sysadmin not a developer. This is more like a hobby.
Yes I have been stung by this in the past but with Regex. I think I
was told i was calling it in the wrong context and I should use a
slice.
> 1. List-Context => returns list of values:
> ---------------------------------------------
> Example for list contexts:
> @vals = $r->param("keys")
> foreach( $r->param("keys") )
> @hash_slice{$r->param("keys")}
>
> 2. Scalar-Context => returns first value of list:
> -------------------------------------------------
> $val = $r->param("keys")
> $hash{$r->param("keys")}
>
>
> > Still not sure why I can do
> >
> > my $r=Apache::Request->new(shift);
> > my @users;
> > foreach my $param ($r->param) {
> > push(@users,$r->param($param));
> > }
>
> here you are in list context.
>
> @user{$apr->param("keys")} = ();
> map { $user{$_} = 0 } keys %user;
This looks tastey. I could learn something here. I think that is a
slice....not too familar with map....better go back to the books.
This is what was told to consider but to be honest I am not familiar
with the hash slices or maps so I opted to stick the lot in an array
as per your 2nd example.
> > and not:
> >
> > my $r=Apache::Request->new(shift);
> > my %users;
> > foreach my $param ($r->parm) {
> > %users{$r->param($param) = 0;
> > }
> >
>
> That's not valid syntax at all if you meant $user{$r->param($param)} =
> 0 then you are here in scalar context.
Opps, I meant $users{$r->param($param)}. Not that it would have
worked.
Sorry to labour on this. Better get off it now of the list grumble.
Thanx again Tom.
Dp.
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: request appears to be processed twice with PDF::Create
Posted by Tom Schindl <to...@gmx.at>.
Dermot Paikkos wrote:
> Hi,
>
[...]
>
> These reports are actually users clock-in/clock-out times for the
> week. So the reports would are generated weekly printed and rarely
> used again. The issue was that if the HR person uses IE5/6 the
Oh. I see not a very heavy loaded service :-). Are you running the
server on win32?
> That besides, the above method didn't work (see mail to list
> "Accessing form multiples". I think you replied to it. Whenever I
> tried to loop through the incoming list of
> "user=dermot+paikkos&user=joe+blogs" I was only getting the first
> user.
Although that's perl here's the explanation.
because $r->param( "key" ) can be used in 2 contexts
(see perldoc -f wantarray). That's something very specific to
perl which you maybe not familiar with when have used langs
like java, c++, ...:
1. List-Context => returns list of values:
---------------------------------------------
Example for list contexts:
@vals = $r->param("keys")
foreach( $r->param("keys") )
@hash_slice{$r->param("keys")}
2. Scalar-Context => returns first value of list:
-------------------------------------------------
$val = $r->param("keys")
$hash{$r->param("keys")}
> Still not sure why I can do
>
> my $r=Apache::Request->new(shift);
> my @users;
> foreach my $param ($r->param) {
> push(@users,$r->param($param));
> }
here you are in list context.
@user{$apr->param("keys")} = ();
map { $user{$_} = 0 } keys %user;
>
> and not:
>
> my $r=Apache::Request->new(shift);
> my %users;
> foreach my $param ($r->parm) {
> %users{$r->param($param) = 0;
> }
>
That's not valid syntax at all if you meant $user{$r->param($param)} = 0
then you are here in scalar context.
> I am sure you code would work fine but what I have managed to do
> seems to work in both IE and firefox and is code I can manage. If I
> got stuck on yours I would out of my depth. So a sincere thanks but I
> am going to stick with what I have for now.
> Thanx.
> Dp.
>
>
Reclaim Your Inbox!
http://www.mozilla.org/products/thunderbird
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html
Re: request appears to be processed twice with PDF::Create
Posted by Dermot Paikkos <de...@sciencephoto.com>.
Hi,
Perhaps your right but let me explain what the problem is again
because you obviously far better at this and I am not even a good
perl scripter so your code makes little sense and I am not sure I see
the problem you are trying to fix.
These reports are actually users clock-in/clock-out times for the
week. So the reports would are generated weekly printed and rarely
used again. The issue was that if the HR person uses IE5/6 the
request is sent twice so you end up open the users file twice and
looping though the data twice. The only consequence of this is the
lines on the PDF are almost in bold as each line is sent twice. What
I was trying to do was create a hash with the user's name as a key
($users{"Dermot Paikkos"} = 0) and then increment the value once the
file was read so I didn't open the file a second time.
I am not sure if the child process would really come into it but I
bow to your expertise on that.
One of the odd things about this was that although the request could
be read as coming in twice, if the HR person selected 4 users, you
still get 4 pages. So no biggy there.
That besides, the above method didn't work (see mail to list
"Accessing form multiples". I think you replied to it. Whenever I
tried to loop through the incoming list of
"user=dermot+paikkos&user=joe+blogs" I was only getting the first
user.
Still not sure why I can do
my $r=Apache::Request->new(shift);
my @users;
foreach my $param ($r->param) {
push(@users,$r->param($param));
}
and not:
my $r=Apache::Request->new(shift);
my %users;
foreach my $param ($r->parm) {
%users{$r->param($param) = 0;
}
I am sure you code would work fine but what I have managed to do
seems to work in both IE and firefox and is code I can manage. If I
got stuck on yours I would out of my depth. So a sincere thanks but I
am going to stick with what I have for now.
Thanx.
Dp.
On 4 Aug 2004 at 9:53, Tom Schindl wrote:
> Hi,
>
> what you really want is an intelligent caching behaviour:
>
> 1. Generate PDF if not exists in Filesystem (the advantage is that
> multiple processes have a access to this PDF) or BerkleyDB for
> example
> 2a. Sent Generatated PDF from Cache
> 2b. Regenerate PDF if underlying data has been modified and restore
> for
> later
> [3. A cronjob which cleans up your disk-cache from time to time]
>
> Code looks like the following(Apache2):
>
> -------------------------8<-------------------------
> use Apache::RequestIO ();
> use Apache::RequestRec ();
> use Apache::Constants ();
>
> sub handler {
> my $r;
> ## ....
> my $cache_key = md5( $user . $path );
> my $file = "/tmp/pdf-cache/$cache_key.pdf";
> my $rc;
>
> if( -e $file && stat($_[0])[9] > stat($path)[9] ) {
> eval {
> $r->sendfile($file);
> }
>
> if( ! $@ ) {
> return Apache::OK;
> }
> }
>
> ## create your PDF like you've done before
> ## ....
>
> open( CACHEFILE, ">$file" );
> print CACHEFILE $content;
> close( CACHEFILE )
> }
> -------------------------8<-------------------------
>
>
> Dermot Paikkos wrote:
> > Hi Tom,
> >
> > I think I see what your saying. What if you ignored the first and
> > worked on the 2nd? Same problem I guess if you hit the first child.
> > I haven't tried to determine if there are two processes as a result
> > of hitting the form but I know from the logs that the module starts
> > cycling through the form data again.
> >
> > It is probably very sloppy but my problem was that given a username
> > and their file, the file was being opened twice which made parsing a
> > bit tricky. All I want is to open the file once. I am open to more
> > elegant suggestions though :-) Dp.
> >
> >
> > On 30 Jul 2004 at 2:41, Tom Schindl wrote:
> >
> >
> >>Hi,
> >>
> >>Are you sure that's working as you expected? If I got you right I'm
> >>afraid I have to disappoint you but this only works if the second
> >>request is handled by the same apache-child as the first one and
> >>there's another problem. If you come back one day later and hit the
> >>same apache-child which already processed the first request, you
> >>won't get anything.
> >>
> >>Tom
> >>
> >>Dermot Paikkos wrote:
> >>
> >>
> >>>Arnaud,
> >>>
> >>>I have found a way around this. I don't know if your interested but
> >>>it goes likes something like this:
> >>>
> >>> foreach my $param ($r->param) {
> >>> if ($param =~ /\busers\b/) {
> >>> $users{$r->param($param)} = 0;
> >>> }
> >>>....snip...then later
> >>>
> >>> foreach my $key (keys %users) {
> >>> next if ($users{$key} == 1;
> >>> $users{$key} = 1;
> >>> }
> >>>
> >>>The idea being you only work request that haven't been processed
> >>>yet. Once you process a request you increment that hash key to 1
> >>>and can avoid using it again. IE still sends the request twice and
> >>>it is working with the first request not the second.
> >>>
> >>>Just a thought.
> >>>Dp.
> >>>
> >>>
> >>>
> >>>On 29 Jul 2004 at 16:20, Arnaud Blancher wrote:
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>>Dermot Paikkos a écrit :
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>>Does this mean you have to go an clean up these files later
> >>>>>
> >>>>>
> >>>>>
> >>>>
> >>>>yes, if you dont want they stay on the disk.
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>>or is
> >>>>>this done when the process ends?
> >>>>>
> >>>>>
> >>>>>
> >>>>
> >>>>maybe you can write a special handle for the directory where you
> >>>>ll write your pdf that delete the pdf when the connection (due to
> >>>>the redirect) will be close by the client (but i'not sure).
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>>I don't want to slow the users down
> >>>>>unless I have to.
> >>>>>
> >>>>>I think I would like to determine the user-agent and work around
> >>>>>the repeating requests....somehow. Do you know how to find out
> >>>>>the user- agent when using Apache::Request? I can't see it when
> >>>>>I use this object. Thanx. Dp.
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>
> >>>>
> >>>>
> >>>
> >>>
> >>>~~
> >>>Dermot Paikkos * dermot@sciencephoto.com
> >>>Network Administrator @ Science Photo Library
> >>>Phone: 0207 432 1100 * Fax: 0207 286 8668
> >>>
> >>>
> >>>
> >>>
> >>
> >>
> >>--
> >>Report problems: http://perl.apache.org/bugs/
> >>Mail list info: http://perl.apache.org/maillist/modperl.html
> >>List etiquette: http://perl.apache.org/maillist/email-etiquette.html
> >>
> >>
> >
> >
> >
> > ~~
> > Dermot Paikkos * dermot@sciencephoto.com
> > Network Administrator @ Science Photo Library
> > Phone: 0207 432 1100 * Fax: 0207 286 8668
> >
> >
>
>
> Reclaim Your Inbox!
> http://www.mozilla.org/products/thunderbird
>
> --
> Report problems: http://perl.apache.org/bugs/
> Mail list info: http://perl.apache.org/maillist/modperl.html
> List etiquette: http://perl.apache.org/maillist/email-etiquette.html
>
>
~~
Dermot Paikkos * dermot@sciencephoto.com
Network Administrator @ Science Photo Library
Phone: 0207 432 1100 * Fax: 0207 286 8668
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html