[Perl] Net::Dbus and thread

Guillaume lomig42 at gmail.com
Sat Jul 4 10:48:53 PDT 2009


Hi all,

I am trying to use Net::DBus with threads on a perl script, and I have
a hard time doing this.

If I create my Net::DBus::RemoteObject outside of the threads it will
be used on, I have errors. I thus tried to create one object per
thread, and I have the same behaviour. Here is a small example script
and its output.

#!/usr/bin/perl

use strict;
use warnings;
use threads;

my $th1 = threads->create(\&run, 1);
my $th2 = threads->create(\&run, 2);

sleep;
exit;

sub run {
 my $num = shift;
 my ($bus, $service, $purple);

 eval {$bus = Net::DBus->session};
 if ($@) {print "th$num: Error in bus: $@\n"; threads->exit()}

 eval {$service =
     $bus->get_service('im.pidgin.purple.PurpleService') };
 if ($@) {print "th$num: Error in service: $@\n"; threads->exit()}

 eval {$purple =
     $service->get_object('/im/pidgin/purple/PurpleObject',
                           'im.pidgin.purple.PurpleInterface')};
 if ($@) {print "th$num: Error in purple: $@\n"; threads->exit()}

 eval {$purple->connect_to_signal ('ReceivedImMsg', \&read_cb)};
 if ($@) {print "th$num: Error in connect: $@\n"; threads->exit()}

 eval {Net::DBus::Reactor->main()->run()};
 if ($@) {print "th$num: Error in run: $@\n"; threads->exit()}
}

sub read_cb {
 print scalar(@_) , ' params', "\n";
}
#END OF SCRIPT

So basically get bus, get service, get object, set callback and run.
This works very well single threaded. Here is the output of this
particular script:

th1: Error in service: Can't call method "get_interval" without a pack
age or object reference at /usr/lib/perl5/Net/DBus/Reactor.pm line 211.

Thread 1 terminated abnormally: Can't call method "get_interval" without
a package or object reference at /usr/lib/perl5/Net/DBus/Reactor.pm line 211.

th2: Error in service: org.freedesktop.DBus.Error.NoMemory: Not enough memory

Thread 2 terminated abnormally: main=HASH(0x9ffd850) at
/usr/lib/perl5/threads.pm line 101.
#END OF OUTPUT

Is there a way to create the 'same' object twice, in two different
threads? That is all I am after, as the different threads will not
share those Net::DBus::RemoteObjects. The communication will be done
through shared data structures which will contain only scalars.

Thanks a lot for any help or pointer on this.

Guillaume


More information about the dbus mailing list