in reply to How to pass sockets to different threads?

My reading of your problem is that you need to use more than 64-110 sockets in a program.

The best solution is to use an event driven framework for your program. As suggested previously, the POE (poe.perl.org) framwork is great. I use it extensively. I use it regularly to connect and interact with hundreds of sockets simultaneously. Actually, I've done upwards of two thousand concurrent sockets, but that was just to connect and disconnect quickly across several thousand hosts.

My opinion on Perl Threads is that perl5-dev has not figured out how to do threads well yet and the code is shaky. Clearly, perl5-dev chose not to "share-all", and you can do "share-none" quite nicely with fork(). So they are playing games with "share-some" and "share-explicitly". Figuring out what memory to share seems to me very difficult in a generic case.

You need to know that a perl file handler is a perl memory thing that contains a file descriptor, aka $fh->fileno(). Even if you are using perl threads, you need to pass the file descriptor then on the receiving end wrap the file descriptor in a perl file handle ala IO::Handle->fdopen($fd) .

You might be able to pass the file descriptor from one thread to another with Thread::Queue cuz the file descriptor is just an integer and perl threads are really all in the same process.

However, if you use separate processes via fork() you need to pass the file descriptor over a Unix Domain socket. See Advanced Programming in the Unix Environment by W. Richard Stevens. The chapter is "Advanced Interprocess Communication" section "Passing File Descriptors".

However you are in luck. There is a CPAN module tested under Linux and Solaris for passing file descriptors called Socket::PassAccessRights.

But like I said, use POE. There might be a learning curve, but it is totally worth it, you will thank me. Barring that, stop using perl thread and use processes, then do this file descriptor passing thing.


Good Luck.

Replies are listed 'Best First'.
Re: Re: How to pass sockets to different threads?
by noslenj123 (Scribe) on Jan 03, 2004 at 05:57 UTC
    Thanks for the tips gang. I have started taking a look at POE now and it looks promising. There definately is a learning curve there. I'll look around for a good tutorial if there is one. Any suggestions?