Naive thread question. 
Author Message
 Naive thread question.

This is just killing me.   I need to do a bunch of shell commands in parallel.
I want to get the output if there is any.   I have tried Thread.New,
but contrary to the docs, I can't seem to use an instance variable to pass
data out of the thread.  Globals work.   I want my node object to have access
to this command output, but in order to execute in parallel I have to use
threads or fork.   I have tried passing instance and local variables into the

def command ( the_command )
    t = Thread.new do
        sin, sout, serr =  Open3.popen3( the_command )
      sout.each_line { |line| puts line }  # prints output to stdout

      sout.close
    end # of Thread


  end # def command



Sun, 04 Jul 2004 03:31:51 GMT  
 Naive thread question.

Quote:

> def command ( the_command )
>     t = Thread.new do
>         sin, sout, serr =  Open3.popen3( the_command )
>       sout.each_line { |line| puts line }  # prints output to stdout

>     end # of Thread


>   end # def command

My first guess here is that your "sout.each_line ..." is reading
all the output.  Hence, when you get to the sout.readlines, there
is nothing to read (since each_line has already read it all) and

I'd try getting rid of the the each_line code.



Sun, 04 Jul 2004 06:44:44 GMT  
 Naive thread question.

I just realised what the problem is!  You've been bitten by
the "threads don't abort on exception" problem that has caused
me confusion about fifty times now.

The real issue is that you need a "require 'open3'" at the top.

What's happening is that the thread is throwing an exception,
because Open3 is an undefined symbol, but you can't see that's
happening, because, by default, when a thread throws an exception,
it just dies quietly, rather than aborting with a stack trace.

I recommend setting Thread.abort_on_exception to true during
your testing phase for any new thread code.  Once you're happy
that it's "always" going to be OK, take that out.

Anyway, sticking the require at the top should sort it out,
but as I said previously, you'll probably want to get rid
of the each_line code, otherwise your var won't end up with
any data.

I think I've mentioned before that I think the default should
be that abort_on_exception is true, because it causes confusion
every time I write multithreaded code ... of course, maybe most
people are smarter than me and learn after the third or fourth
time :-).



Sun, 04 Jul 2004 07:01:26 GMT  
 Naive thread question.
In trying to show off my problem for Harry, I found a smoking gun.

I was paying no attention to what the main thread did.  For some stupid
reason I was assuming (duh!) that it blocked.   I've been trying to read
uninitialized data.

I didn't see that until I laced the whole program with print statements and
watched the control flow rather differently from what I expected.

I bet it's going to work right when I get the execution flow in the main
thread fixed.

Well, I learned something!

Eirikur



Sun, 04 Jul 2004 07:47:50 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. naive type checking II (not so naive?)

2. Questions from a naive user

3. Questions from a naive st programmer

4. Naive Forth questions

5. Extremely naive (and probably stupid) question

6. (Yet another) naive question

7. Naive question

8. Naive question

9. Another naive question

10. Naive Number Question

11. Naive Number Question

12. Naive newbie question.

 

 
Powered by phpBB® Forum Software