zentara schrieb:
>> I wrote a small server, which accepts connections on a tcp socket and 
spawns a 
>> new thread for each connection, which terminates when the socket has been 
>> closed.
>>
>> The server runs fine, however, while stresstesting, I experienced a runaway 
of 
>> the memory consumption.
>> After a few hundred connects and disconnects I'll get an Out of memory. :-(
>>
>> In order to test threads in perl, I wrote a new script which just spawns 
and 
>> terminates threads. ( Attached it below )
>>
>> I already figured out that global variables seem to lead to memory leaks, 
>> however, although the memory size raises slower now, it still raises. ( 
which 
>> I didn't expect ).
>>     
> Hi, sorry to take so long to answer, but I kept getting discouraged
> by the complexity of your code..... alot of interacting while() loops.
>
>   
Sorry for the testcase, today I was confused by the code myself..

> I was just in a discussion with BrowserUk on http://perlmonks.org
> about the need to watch for memory gains in threads. 
> He is adamant that it can be done without reusing threads.
>
> My point was "sometimes for some weird reason" you need to reuse
> threads instead of making them "throwaways".
>
> Your conclusion about creating a pool at startup is how I would do it.
> Although, I would make a pool of threads, instead of preforked.
> Create the threads, and put them into a sleep loop, then used shared
> variables to wake them as needed. As they are done with their task, put
> them back onto "ready queue" and put them back to sleep.
>
> Your memory use should stay constant this way.
>
> I would ask your question on http://perlmonks.org and see what they say.
>   
Thanks for your encouragement, mainly I was in doubt whether my test 
code has some bugs or not.

I searched cpan for existing modules which manage a thread pool, but 
didn't like what I found.
Mainly because the code to run by the threads is handled in an eval 
expression.
I did some profiling and testing, if I hand over just a function's name 
to  be run  that's about at least 10 times faster.
I'd guess the difference will grow with the script's size.


I dived into it and wrote some code which manages a pool of threads and 
can also store the results.

There are two more questions.


If I got it right it's not possible to make references shared, 
especially references to functions / modules..?

So I'm doing something like the pseudo code below in order to hand over 
the code to be runned in the threads.

-----
sub function{ do something }

sub dosomework{
my $f = shift;
&$f( @_ );
}

&dosomework( "function", @arguments );
-----

Although something like &dosomework( "modulename::function", @args ) is 
possible, I wasn't able to figure out how to submit objects to an other 
thread, I guess it's not possible ?




Since I did the work, I'd possibly like to publish it, I believe it 
could be useful to others.
Is there a recommended way / would you recommend it at all ?


Michael

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to