Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Perl detached threads, loops & my?

by rjt (Deacon)
on Jun 08, 2010 at 23:59 UTC ( #843762=note: print w/replies, xml ) Need Help??

in reply to Perl detached threads, loops & my?

We need more information. What does @go_do_something contain? How many threads are you trying to create? What are they doing? In other words, can you give us a runnable but minimal example that exhibits the behavior on your system?

Memory leak? Maybe, but, again, a real example would be necessary, here, plus some information on how you're measuring the memory and determining there is a leak.

You said it "crashes the program without any error message". Is it possible that your script just decided it was done creating threads and exited on its own? That would produce behavior consistent with what you are seeing. Since you've detached every thread, once your @go_do_something loop exits, how do you know (in your code) that all of your threads have finished and it is safe to exit your program? What was its exit status when execution was complete? I might be completely missing the issue, here, but with the limited information available, this really seems most likely to me.

Maybe you have a valid reason for detaching everything, but if you want to be sure all threads have finished, you might consider something along the lines of:

use warnings; use strict; use threads; use feature 'say'; my @go_do_something = (1 .. 10); sub some_sub { say "Thread #$_ started"; sleep(rand(10)); say "Thread #$_ complete"; } my @threads; push @threads, threads->new(\&some_sub, $_) for @go_do_something; say "All threads started."; $_->join for @threads; say "All threads joined. Exiting.";

Replies are listed 'Best First'.
Re^2: Perl detached threads, loops & my?
by expresspotato (Beadle) on Jun 09, 2010 at 01:56 UTC
    @BrowserUK - I am using the latest version of perl (why would anyone use anything less?)
    The only reason I "store them" is to ensure it is a fresh thread everytime. Using my $thr -> will eventually crash.
    The script is designed to do one thing. Process the list of tasks and just keep on going. Tasks (detached threads) may take a second and they may take an hour.
    I do not claim it takes "Hours to run", I claim it may be "run for hours".
    Ah ha! See... Always calling my $thr on occasion seems to overwrite / invalidate the older thread. When checking memory usage using my $thr, it never seems to increase which is very odd to me.
    Thanks for the code, however the script is "always running" and does not ever exit.
    Perhaps the actual code will shed some light on what is really going on. Its attached below. Keep in mind the same problem still exists. How to ensure this is a FRESH thread (as my or the same $thr will eventually crash), without wasting all that memory.
    use threads; do("./"); #NOT INCLUDED IN EXAMPLE $thr4 = threads->new(\&processor); $thr4->detach(); sub processor{ my $select; $select = qq~ select * from pool_process where any="1" and status +="" order by id asc limit 1; ~; while (1){ if ($sys_ok == 1){ sleep(1); my @row = &row_sql($select,-1); if (!(@row)){ #pass }else{ &del_sql(qq~ update pool_process set status = "RUNNING",proc +_srv="$server_id" where id="@row[0]"; ~,-1); print "Processing (@row[1],@row[2],@row[3],@row[4])\n"; $used_threads++; my $thr =threads->new(\&process_start,@row[1],@row[2],@row[3 +],@row[4],@row[5],@row[6],@row[7],@row[0]); $thr->detach(); if (@row[4] eq "generate_details"){ sleep(3); } } }else{ sleep(30); } } } sub process_start{ my $return_code = system(qq~ perl ./ "$_[0]" "$_[ +1]" "$_[2]" "$_[3]" "$_[4]" ~); }

      @expresspotato. This ain't twitter.

      What you are doing makes no sense.

      What you say you need: "FRESH threads" makes no sense.

      And if you cannot be bothered to answer my questions properly, then I can't be bothered to help.

      Good luck.

      This is obviously neither the real code nor an example that we can run. Please, help us help you. Read the responses you've been given, and give us a minimal and runnable! example (with inputs and expected outputs) with strict and warnings that we can use to reproduce the undesired behavior that you see. Until then, we can't effectively help you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://843762]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-06-22 04:18 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.