# Nginx + embedded perl, sharing variables across workers

**URL:** <https://www.ruby-forum.com/t/nginx-embedded-perl-sharing-variables-across-workers/205450>\
**Category:** NGINX\
**Created:** [April 6, 2011, 11:29pm UTC](https://www.ruby-forum.com/t/nginx-embedded-perl-sharing-variables-across-workers/205450 "2011-04-06T23:29:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [April 6, 2011, 11:29pm UTC](https://www.ruby-forum.com/t/nginx-embedded-perl-sharing-variables-across-workers/205450/1 "2011-04-06T23:29:40Z")

</div>

Hi,

I have written a perl handler that generates a random value and needs to  
maintain the same value consistently across all other perl handlers (as  
there is 1 perl handler per nginx worker) until the workers are  
restarted;

I’ve attempted to do so by issuing $r-\>variable(‘varname’, $rand) to no  
avail - is it impossible to set a variable defined in nginx.conf and  
have the new setting affect any further reads of that variable from  
other workers?  
Which options do I have if I’d like to share $varname across multiple  
perl handlers?

Thanks!  
Yuri

Posted at Nginx Forum:

> **[Nginx + embedded perl, sharing variables across workers](https://forum.nginx.org/read.php?2,188905,188905#msg-188905)**
>
> Hi, I have written a perl handler that generates a random value and needs to maintain the same value consistently across all other perl handlers (as there is 1 perl handler per nginx worker) until the workers are restarted; I've attempted to do so by...

---

<div class="post-metadata">

**Author:** ![yuri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/71c47a/32.png) [@yuri](https://www.ruby-forum.com/u/yuri)\
**Post date:** [April 7, 2011, 1:18am UTC](https://www.ruby-forum.com/t/nginx-embedded-perl-sharing-variables-across-workers/205450/2 "2011-04-07T01:18:28Z")

</div>

Hello!

On Wed, Apr 06, 2011 at 05:28:51PM -0400, yuri wrote:

> other workers?  
> Which options do I have if I’d like to share $varname across multiple  
> perl handlers?

Request variables ($r-\>variable) are per-request, not even  
per-worker. Normal perl variables are per worker as worker uses  
one perl interpreter.

To share data between multiple workers use normal IPC facilities  
provided by perl (but keep in mind blocking for too long isn’t  
good idea). Something like IPC::Shareable / IPC::Mmap::Share  
should do the trick.

Maxim D.
