Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Can I/O operations on the same IO::Socket be executed in different threads?

by Anonymous Monk
on May 13, 2015 at 02:35 UTC ( [id://1126492]=note: print w/replies, xml ) Need Help??


in reply to Can I/O operations on the same IO::Socket be executed in different threads?

This should be portable (works on window ) .... dunno if its better or worse than using Thread::Queue for ipc( qin/qout)

#!/usr/bin/perl -- use strict; use warnings; use threads stack_size => 4096; use IO::Socket::INET; our $host = "localhost:".int(rand 99999); async( sub { my $tid = threads->tid; use Time::HiRes qw/ usleep /; my $server = IO::Socket::INET->new( Proto => "tcp", LocalHost => $host, Listen => 1, Reuse => 1, ) or die "Cannot start tcp server: $!\n $@\n "; print "listening on $host\n"; sleep 1; my $client = $server->accept(); while(<$client>){ print "reader($tid) $_"; last if /exit/i; } }); async( sub { my $tid = threads->tid; use Time::HiRes qw/ usleep /; my $socket = IO::Socket::INET->new( Proto => "tcp", PeerHost => $host, ) or die "Cannot start client : $!\n $@\n "; for(1..20){ usleep 2000*rand(500); # 2000 sleep for 2 millisecond $socket->print( "writer($tid) ".scalar(gmtime)."\n" ); } $socket->print( "writer($tid) ".scalar(gmtime)." exit\n" ); }); $_->join for threads->list; __END__
  • Comment on Re: Can I/O operations on the same IO::Socket be executed in different threads?
  • Download Code

Replies are listed 'Best First'.
Re^2: Can I/O operations on the same IO::Socket be executed in different threads?
by Anonymous Monk on May 14, 2015 at 00:12 UTC

    And? You've got two connected endpoints, two descriptors. There's no shared socket.

      And? You've got two connected endpoints, two descriptors. There's no shared socket.

      Yes, that is the point of this example -- it does seem to miss the point of the OP, so yeah :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1126492]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (1)
As of 2024-04-19 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found