in reply to Module development: concurrent versions (Updated)
If you were in Java I would say, load each class in a seperate sibling class loader and put any classes shared in common into a parent class loader. Java identifies classes using class loader + name, so even if two classes have the same name if you've loaded them into sibling class loaders Java won't confuse their inheritance chains and inter-class relationships.
Although Java is not Perl, and Perl is not Java, it appears you can use Perl threads in much the same way as the Java class loaders. Each thread gets its own copy of @INC and %INC. Furthermore, each thread inherits the contents of @INC and %INC from its parent thread, as illustrated in this little demo:
use strict; use warnings; use threads; use threads::shared; our @STARRING_CLASSES=qw(Carp.pm List/Util.pm Test/More.pm); my @aBig; my $aBig=bless(\@aBig,"MaybeSharable"); my $aBigShared=&share($aBig); my $t; require Carp; $t = threads->new(sub { require List::Util; print "-------------------------------------\n" , "tid=".threads->tid . " \\\%INC=".\%INC . " \\\@INC=".\@INC."\n"; while(my($k,$v)=each(%INC)) { $k="**$k" if grep{$k eq $_} @STARRING_CLASSES; print "$k=$v\n" }; print "\n"; }); $t->join(); $t = threads->new(sub { require Test::More; print "-------------------------------------\n" , "tid=".threads->tid . " \\\%INC=".\%INC . " \\\@INC=".\@INC."\n"; while(my($k,$v)=each(%INC)) { $k="**$k" if grep{$k eq $_} @STARRING_CLASSES; print "$k=$v\n" }; print "\n"; }); $t->join();
Given that, why not just use two sibling threads in your program and load one version in thread 1 and the other in thread 2?
Update: changed choice of classes to use in demo so that there is at least one package each thread loads that the other does not. (Turns out Scalar::Util pulls in List::Util, the two modules used in the original version - who knew!).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Module development: concurrent versions (Updated)
by BrowserUk (Patriarch) on Dec 24, 2010 at 00:16 UTC | |
by ELISHEVA (Prior) on Dec 24, 2010 at 05:48 UTC | |
by BrowserUk (Patriarch) on Dec 24, 2010 at 10:47 UTC |