in reply to Is is possible to use perl fork and ithreads within the same application

I understand questioning the sanity of mixing forks and threads. The reason for wanting to mix the two is that I have to open up 300+ connections to unique devices. When I do this using threads, the process size grows to 2-3G due to the nature of ithreads. Once the threads are closed, perl stays at that memory size until the process has exited. I have done a lot of reading and the conclusion seems to be that perl never give memory back to the OS until the process is done running. In my case, this process stays running for weeks.

Now, if I use forks, once the linux threads(forks) are done, the memory is given back to the OS.

So, I thought, let's just use forks everywhere! this would be great as they will give the memory back and they use COW. But, I found that some of the code in this program uses globs and forks doesn't support globs therefore I can't use forks for other sections of this program.

I only see 2 viable solutions: 1. use forks and threads (hence the reason for my post) 2. use a thread pool for the section of code that opens up the 300+ connections and limit the number of threads at any given time to some value a lot lower than 300. I am going to prototype using a thread pool and see if the open time is still acceptable.

But, if I was able to unload forks and reload threads it would make my life easier :-)

Thanks much
  • Comment on Re: Is is possible to use perl fork and ithreads within the same application

Replies are listed 'Best First'.
Re^2: Is is possible to use perl fork and ithreads within the same application
by BrowserUk (Patriarch) on Feb 13, 2015 at 20:44 UTC
    I have to open up 300+ connections ... the process size grows to 2-3G

    Hm. The following code starts 300 threads, each of which opens a connection to a local echo+1 server. The total memory usage is under 700MB:

    #! perl -slw use strict; use IO::Socket; use Time::HiRes qw[ sleep ]; use threads stack_size => 4096; ## Tip: This significantly reduces th +reads memory usage $\ = $/ = chr(13) . chr(10); sub connection { my $svr; do { $svr = IO::Socket::INET->new( 'localhost:12345' ); } until $svr; while( 1 ) { my $msg = int( rand 2**30 ); my $reply; print $svr $msg; $reply = <$svr> until $reply; chomp $reply; warn 'Mismatch' unless $msg + 1 == $reply; sleep 0.001; } } async( \&connection )->detach for 1 .. 300; sleep 1000;

    The other memory saving tip is: only use those packages you need within your threads, before creating them. Load (require) anything that you only need in main after you've created your threads.

    The threaded echo+1 server for your testing:

    #! perl -slw use strict; use threads stack_size => 4096; use IO::Socket; $\ = $/ = chr(13).chr(10); my $lsn = IO::Socket::INET->new( Reuse => 1, Listen => 1e6, LocalPort +=> 12345 ) or die "Server failed to create listener: $^E"; print "Server listener created"; while( my $client = $lsn->accept ) { print "Server accepting client connection"; async { while( my $in = <$client> ) { chomp $in; print $client $in + 1; printf "\rServer echoing client input: '%s'", $in; } print "Server shutting down"; shutdown $client, 2; close $client; }->detach; }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re^2: Is is possible to use perl fork and ithreads within the same application
by shmem (Chancellor) on Feb 14, 2015 at 10:23 UTC
    The reason for wanting to mix the two is that I have to open up 300+ connections to unique devices.

    You don't tell what kind of connection that is, and what amount of code is involved interacting with those devices. Maybe you could make the connections into just (file)handles using pipe before forking, and IO::Select in the parent. If the creation and interaction with those devices is asynchronous (i.e. random wrt time), you could use an event based model using Event, EV or POE. That way your forked processes will be small enough, and will be returning claimed memory to the OS after execution. This is given with a lot of handwaving, of course; more of your code is required to give good advice.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^2: Is is possible to use perl fork and ithreads within the same application
by LanX (Saint) on Feb 13, 2015 at 19:42 UTC
    > But, I found that some of the code in this program uses globs and forks doesn't support globs

    you mean typeglobs ? Can't you get rid of them?

    Cheers Rolf

    PS: Je suis Charlie!

Re^2: Is is possible to use perl fork and ithreads within the same application
by Anonymous Monk on Feb 13, 2015 at 21:00 UTC

    Once the threads are closed, perl stays at that memory size until the process has exited.

    Or until you join the threads, join for threads , it makes the memory reclaimable