use threads; use SOCKETSVR::Server; my $port = 5188; my $d = new SOCKETSVR::Server LocalPort => $port; print "Socket Server started. port=$port\r\n"; while (my $conn = $d->accept) { # $conn is socket client handler, which is GLOBtype threads->new(\&start_process, $conn)->detach(); undef($conn); } sub start_process { my $conn=shift; if(my $r = $conn->recv_data) { #blabla.... } $conn->close; return; }
This program will create a thread for any socket client connection. But I found the memory is increasing rapidly, and the program will crash after have run for about 3 days.
In the past weeks, I tried a lot of solutions for this problem:
1. I updated Perl from v5.8.8 to v5.10.1, and threads from v1.0.7 to v1.7.5 ----It didn't work.
2. I used Thread::Pool module to avoid massive threads open and close, which might have caused memory leak. ---It prompted error: Can't store GLOB items at lib/Storable.pm. That's because $conn above is GLOB value
3. I tried to transfer fileno($conn) between threads and re-construct socket client by open my $conn, "+<&=$fno"; ---It didn't work. fileno($conn) always returns 9 and socket client can't be re-opened.
4. I used some other threadpool module ---They didn't work for $conn cannot be shared. it's not a simple data structure.
So after so many tries, I still cannot make it. Can any Monk help me? Thanks!
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |