adamcpfeiffer has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have one module that I would like to use forks and another I would like to use ithreads. Here is pseudo code for what I want to do:
Module1 Use threads; Use threads::shared; Use Module 2; sub thread_it { Code that uses threads/threads::shared } sub fork_it { my $mod2 = Module2->new() $mod->fork_it() }
Module2 Use forks; Use forks::shared; sub fork_it { code that runs forks here }
Main script
my $mod1 = Module1->new() $mod1->fork_it() $mod1->thread_it()
The problem is that once forks and forks::shared loads, they redefine the functions of threads and threads::shared. Once I am done with fork_it(), how do I redefine the functions back to threads and threads::shared? Thanks much

Replies are listed 'Best First'.
Re: Is is possible to use perl fork and ithreads within the same application
by Corion (Patriarch) on Feb 13, 2015 at 07:46 UTC

    I think the general solution is not to mix threads and fork. No good can come of it as the two mechanisms don't interact well.

Re: Is is possible to use perl fork and ithreads within the same application (forks and threads)
by Anonymous Monk on Feb 13, 2015 at 03:19 UTC
    Hmm... seeing how the entire purpose behind "forks.pm" is to emulate "threads.pm" .... why even bother?
Re: Is is possible to use perl fork and ithreads within the same application
by adamcpfeiffer (Novice) on Feb 13, 2015 at 19:32 UTC
    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
      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
      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'
      > 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!

      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

Re: Is is possible to use perl fork and ithreads within the same application
by locked_user sundialsvc4 (Abbot) on Feb 13, 2015 at 14:01 UTC

    I agree:   stick to one or the other.   Categorically speaking, the success of a multi-threaded application entirely depends on the ability of the various threads to synchronize their activities with one another when they have to.   All, in this case, within the operating context of a single Perl interpreter instance?!   The outcome, at the very best, would be “uncertain,” and I detest the word “uncertainty” in this context.