how to share socket handle across threads?



  • 1. How to move perl from one PC to another PC (windows XP)
    We have a PC/Windows XP running some Perl scripts for daily routine. I'd like to copy them to another PC/ windows xp, for running same perl scripts. What should I do to the new PC set a path or ??
  • 2. Tie::IxHash::Easy with "eval()"
    Hi, I'd like to use "Tie::IxHash" to maintain the order of keys in a hash. Since the hash can contain other hashes (and so on), I need to use "Tie::IxHash::Easy" so that the nested hashes also maintain their ordering. I'm reading the entire hash from a file. The format of the file is exactly like Data::Dumper so that I can build the hash by simply saying: eval($file_contents); This fails because there's no way to "tie" the internal workings of eval to "Tie::IxHash::Easy" so it always just returns a normal hash. Copying the returned into an IxHash has no effect because the ordering is already lost. So, how can I load an IxHash from a file in Data::Dumper format and preserve its order (including nested hashes)? Thanks.
  • 3. CGI::Carp die with status
    Hi, Has anyone experience with dying from CGI::Carp with a status? Using LWP one can catch status with: $response = $ua->request($req); $content = $response->content; if ($response->is_success) { print $content; } else { print $response->status_line; } However, when the script "dies" with CGI::Carp the exit status is "200 OK". I'd rather have a status that is not "OK" and not scan the returned $content for "Software error". Has someone been able to do this? Bernd

how to share socket handle across threads?

Postby seb » Wed, 09 Jul 2003 07:25:46 GMT


I've been rtfm'ing for days but can't a solution to this problem:

I have a thread that receives multiple client request thanks to an
endless loop through the accept() method. The new socket object
returned by accept() must be communicated to another master thread. I
am using the IO::Socket module.

When trying to use threads::shared or Thread::Queue, I keep getting
this failure:
Invalid value for shared scalar at ./ line N.

So is there a way to share a socket/file handle accross threads?

Thank you


Similar Threads:

1.Sharing socket handles accross processes or threads


I have been using Net::SMTP::Server::Net

I want to pass client handles (Net::SMTP::Server::Net::Client) to
another process or thread.

I naivly expected to use threads and global memory but in Perl (as it
turns out) I can only share scalars, hashes and arrays.  The
Net::SMTP::Server::Client->new($conn) is a glob.  I am not 100% sure I
know exactly what this means!

Is there any way that I can pass the connection for the
SMTP::Server->accept accross processes or threads or do I have to spawn
a new process for each instance?


2.share semaphore across threads

3.threads & threads::shared & Threads::Semaphore

Hi, All,

Hope everyone's new year is starting out very well.

I'm having to adjust a perl program I wrote to manipulate some  
genetics data.  Originally, the program had no memory problems but now  
that I've added a couple more hashes, I'm having memory issues.  It  
now runs out of memory when it's about half way through processing the  
data.  Unfortunately, the data is very interconnected and the  
statistics I need to execute involves data from several of the  
hashes.  I'm thinking of using threads & threads::shared in order to  
be able to process, store and access the data among several of 8  
processors on a Sun Spark system.  Of course, I'll need to install the  
threads and threads::shared modules and possible even re-compile perl  
on this machine but before I go and do all this fun stuff, I wanted to  
ask your opinion about whether or not I'm going down the right rabbit  
hole or if I'm just digging myself a shallow grave.  Would this be the  
way you might do it?  I've also heard of Semaphores.  Might this be a  
better way to go about spreading the data in hash form among several  
processors on one machine and still be able to access the data in each  
hash from the main program?

Thanks in advance for the excellent input I always get from this  
group!  :-)

Aimee Cardenas

4.shared variables in threaded tk not being shared


Wonder can someone help. I've been trying a few things with perl/tk and
threads with some success but cant understand a particular problem.
I've read various posts on the subject and have altered my script
accordingly but still no go! Basically the script creates some shared
variables, a sleeping thread first, then the tk window and widgets.
The thread essentially starts to update a shared variable every second
once the start button is pressed. I can see this by printing to STDOUT.
A label in the GUI points to this shared variable and is instructed to
update it.
However if i put in some debug print statements i can see that in
effect this variable is not shared as it retains the initial value only
hence the label may be updated but with the same value.
Here is the code:

use Tk;
use threads;
use threads::shared;
use strict;

my $go : shared = 0;
my $myValue : shared = 0;

my $anotherThread = threads->new(\&ticker);

sub ticker {
	while (1) {
		if ($go == 0) {sleep 1;}

		else {
			print "$myValue\n";
			sleep 1;

my $mw = MainWindow->new();
$mw->title('UDP Sniffer');

my $f1 = $mw->Frame(-borderwidth => 2, -relief => 'groove')->pack(-side
=> 'top', -fill => 'x');
my $label2 = $f1->Label(-textvariable => \$myValue, -width =>
15)->pack(-side => 'left');
$f1->Button(-text => 'Start', -command => \&start)->pack(-side =>
$f1->Button(-text => 'Stop', -command => \&stop)->pack(-side =>
$f1->Button(-text => 'Reset', -command => \&resetme)->pack(-side =>


sub start {
	$go = 1;
	print "In the main window - $myValue\n";
 	##$mw->repeat(1, sub {$mw->update;});

sub stop {
	$go = 0;

sub resetme {
	$myValue = 0;

Threads are quickly becoming annoying to use as it seems they present
more problems than solutions with tk so could someone tell me what
other options i have for something similar to the above?

Thanks in advance to share variable across multi-processes

On 1/30/06, Jeff Pang < XXXX@XXXXX.COM > wrote:
> hello,lists,
> I have a hash var in my script,and I want to get it be shared across multi-processes.Fox example:
> my %hash;
> while(1)
> {
>     die "can't fork:$!" unless defined (my $child = fork());
>     if ($child==0)
>     {
>         do_something_to_hash(); # here I need to do some operations to the %hash,which is defined globally.These operations include add something to %hash,or do modification or 'delete' to %hash.
>     }
> }
> Is there any suggestion or document can help me to do that?thanks.

You might consider using a dbm to store the hash in the file system. 
Look at dbmopen and DBM::Any.

6. mod_perl: sharing data across httpd childs ?

7. sharing a variable across two scripts.

8. Sharing variables across scripts.

Return to PERL


Who is online

Users browsing this forum: No registered users and 33 guest