[Chicago-talk] Sharing data between processes

Steven Lembark lembark at wrkhors.com
Thu Jan 22 10:35:26 CST 2004

> I've been using threads for this thing.  I'd just like to do it in a way
> that
> doesn't require threads, since thread support isn't built into perl (in
> most cases).  My process is such (pseudo code/process):

5.8 has it if you're using that. The support is a bit
bulky, but does work reliably.

Shared memory allows you to exchange data easily (via
storable). One decent approach is to have the package
name into which the item is blessed built into the data
structure. At that point you can use something like:

    sub shared_obj
        my $shmkey = shift;
        my $shared_memory_item = $shared_memory_items{$shmkey};

        bless $shared_memory_item, $shared_memory_item->{class}
            or croak "Bogus '$shmkey': unable to bless it";

For the most part since shared memory uses storable the
best thing to do is store a fairly simple structure (i.e.,
one that doesn't face deep vs. shallow copy issues. A
single-level hash works nicely for most things, with the
entire hash being stored in the shared memory or delivered
as a message:

	$shared_mem_seg = %hash;


	Parallel::Pvm::pack Dumper %hash;

In that case the other side can extract the shared memory
(standardized via storable) or eval it back into a unit
from Dumper. Note that this will not pass around sub's so
you have to deal with them via some sort of jump table or
only use the result objectively:

	my %hash = whatever;

	my $obj = bless \%hash, $hash->{class};

	# use $obj->can to sanity check the object

One trick I use is to add an "init" sub to any classes
whose objects get passed around the cluster. This gives
one quick sanity check:

	# convert a data strucutre sucked out of a message
	# into an object. the caller passes the appropriate
	# class in as the first argument, whatever's left
	# are the initializing args for that class.

	sub init_shared
		# given that the class exists, it should have both
		# "construct" and "init" methods. easier to check
		# up here and give a more speicfic error message.

		my $class = shift
			or croak "Bogus init_shared: false class";

		my $cons = $class->can('construct')
			or croak "Bogus init_shared: $class cannot construct";

		my $init = $class->can('init')
			or croak "Bogus init_shared: $class cannot init";

		# construct a new, empty object. all this really
		# has to do in most cases is a one-liner to bless
		# an approparite data structure.

		my $obj = $class->$cons;
		# caller gets back the result of initializing
		# the object.
		# becuase the object passed back is
		# not just a blessed copy of the input argument
		# the data structures can be different (for example,
		# the object may be a blessed file handle or
		# lazy object built from data passed across in the
		# mesage. passing @_ allows the initializer to
		# diddle its arguments if necessary (e.g., populating
		# poritons of the data structure that do not move
		# well across the network).

		$obj->$init( @_ )

Steven Lembark                               2930 W. Palmer
Workhorse Computing                       Chicago, IL 60647
                                            +1 888 359 3508

More information about the Chicago-talk mailing list