Global variables in mod_perl

Discussion in 'Perl Misc' started by Gunnar Hjalmarsson, May 17, 2004.

  1. If I run this script:

    $count++;
    print "Content-type: text/plain\n\n";
    print $count;

    20 times under mod_perl, it outputs:

    1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4

    while I would have expected it to output:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    System:
    Linux Fedora Core 1
    Apache 2.0.47
    mod_perl 1.99_12
    perl v5.8.1

    Anybody who has an idea of what I'm missing?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 17, 2004
    #1
    1. Advertising

  2. Gunnar Hjalmarsson <> wrote:
    > If I run this script:

    [...]
    > 20 times under mod_perl, it outputs:
    > 1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
    >
    > while I would have expected it to output:
    > 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    [...]
    > Anybody who has an idea of what I'm missing?


    http://perl.apache.org/docs/2.0/user/coding/coding.html#Shared_Variables

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, May 17, 2004
    #2
    1. Advertising

  3. Gunnar Hjalmarsson wrote:
    > If I run this script:
    >
    > $count++;
    > print "Content-type: text/plain\n\n";
    > print $count;
    >
    > 20 times under mod_perl, it outputs:
    >
    > 1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
    >
    > while I would have expected it to output:
    >
    > 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    apache is traditionally multiprocessed - there is no guarantee that a
    second or subsequent request will be served by the same child. Try

    print "$$ $count\n";

    Mark
    Mark Clements, May 17, 2004
    #3
  4. Mark Clements wrote:
    > Gunnar Hjalmarsson wrote:
    >> If I run this script:
    >>
    >> $count++;
    >> print "Content-type: text/plain\n\n";
    >> print $count;
    >>
    >> 20 times under mod_perl, it outputs:
    >>
    >> 1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
    >>
    >> while I would have expected it to output:
    >>
    >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    >
    > apache is traditionally multiprocessed - there is no guarantee that
    > a second or subsequent request will be served by the same child.
    > Try
    >
    > print "$$ $count\n";


    Aha, thanks!

    As an exercise, just to convince myself, I put together the following
    code:

    use SDBM_File;
    use Fcntl;
    print "Content-type: text/plain\n\n";
    my $file = '/path/to/file';
    my $total;

    $count ++;

    tie my %counts, 'SDBM_File', $file, O_RDWR|O_CREAT, 0644;
    $counts{$$} = $count;
    for (sort keys %counts) {
    print "$_: $counts{$_}\n";
    $total += $counts{$_};
    }
    untie %counts;

    print "\nTotal: $total\n";

    Now the result is as expected:

    10569: 2
    10570: 3
    10571: 2
    10572: 2
    10573: 3
    10574: 2
    10575: 4
    10576: 2

    Total: 20

    Previously I have only played with mod_perl using my IndigoPerl
    installation on Windows 98, which (I suppose) runs as one single process.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 17, 2004
    #4
  5. Gunnar Hjalmarsson, May 17, 2004
    #5
  6. Gunnar Hjalmarsson

    Joe Smith Guest

    Gunnar Hjalmarsson wrote:

    > Glenn Jackman wrote:
    >
    >> http://perl.apache.org/docs/2.0/user/coding/coding.html#Shared_Variables

    >
    > Hmm.. Mark convinced me that it was caused by multiple processes rather
    > than threads. Can you please explain?


    Some operating systems implement threads in a way that they look like
    processes. The value $$ is different in each thread.
    -Joe
    Joe Smith, May 18, 2004
    #6
  7. Joe Smith wrote:
    > Gunnar Hjalmarsson wrote:
    >> Glenn Jackman wrote:
    >>> http://perl.apache.org/docs/2.0/user/coding/coding.html#Shared_Variables

    >>
    >> Hmm.. Mark convinced me that it was caused by multiple processes
    >> rather than threads. Can you please explain?

    >
    > Some operating systems implement threads in a way that they look
    > like processes. The value $$ is different in each thread.


    Okay, thanks. Guess I need to read up on both processes and threads...

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 18, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ian douglas
    Replies:
    0
    Views:
    1,824
    ian douglas
    Aug 19, 2003
  2. Wayne
    Replies:
    2
    Views:
    465
    Wayne
    Nov 11, 2003
  3. nicc777
    Replies:
    2
    Views:
    160
    Richard Gration
    Jul 13, 2005
  4. Replies:
    2
    Views:
    445
    Big and Blue
    Jan 25, 2006
  5. Replies:
    9
    Views:
    91
    -berlin.de
    Sep 19, 2006
Loading...

Share This Page