share a var between workers

85 views
Skip to first unread message

arjan

unread,
Aug 17, 2012, 9:04:01 AM8/17/12
to psgi-...@googlegroups.com
If I use Starman with this code:

my $counter = 0;

my $app = sub {
my $senv = shift;
$counter++;
return [ 200, [ 'Content-Type' => 'text/html' ], [ "counter: $counter" ] ];
};

Each worker gets it's own copy of $counter. The value of $counter depends on the worker serving the request.

Would it be somehow possible to share a variable between workers?

Kind regards,
Arjan.

Tatsuhiko Miyagawa

unread,
Aug 17, 2012, 12:41:42 PM8/17/12
to psgi-...@googlegroups.com
use memcache or some other shared memory mechanism.


Nuba Princigalli

unread,
Aug 17, 2012, 12:49:30 PM8/17/12
to psgi-...@googlegroups.com
Hi Arjan,

Redis' been my "shared-memory of choice" for quite some time, might want to check it out:
* with the counter in a hash http://redis.io/commands/hincrby

Have fun,

Nuba

--
 
 
 

arjan

unread,
Aug 19, 2012, 6:51:58 PM8/19/12
to psgi-...@googlegroups.com, nu...@pauleira.com
Hi Nuba,

Thanks, I know redis, it's really cool. 

But I'm not looking for anything database-like, I want to make a server that has a large hash of bit::vectors and serves information about that collection. Hence I need access to an instantiated variable with instantiated objects. The counter was just a simple example to demonstrate the problem with implementing this with starman. 

So I should either find out how to share it between workers, or I should find some single process solution perhaps.

Kind regards,
Arjan.


Op vrijdag 17 augustus 2012 18:49:30 UTC+2 schreef Nuba Princigalli het volgende:

Tatsuhiko Miyagawa

unread,
Aug 19, 2012, 6:56:01 PM8/19/12
to psgi-...@googlegroups.com
Static data can be shared by initializing them on the parent process (inside your app, with --preload-app option) and then referenced safely via the workers, but you can't update the data in the parent without using some kind of IPC mechanism. It's not just a problem with Starman it's a generic thing with forking multi processes.


--
Tatsuhiko Miyagawa



--
 
 
 

arjan

unread,
Aug 20, 2012, 4:57:41 AM8/20/12
to psgi-...@googlegroups.com
Dear Tatsuhuko,

Thank you, I understand. So, I should be ok, with any non-forking solution, like Twiggy.

Kind regards,
Arjan

Op maandag 20 augustus 2012 00:56:01 UTC+2 schreef Tatsuhiko Miyagawa het volgende:
Reply all
Reply to author
Forward
0 new messages