[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Thread reading from a pipe blocks other threads, why?

Anders Lindgren

3/6/2007 8:11:00 AM

Hi!

I'm implementing an application where the main Ruby program spawns off
a number of external programs using pipes. Each pipe is handled by its
own thread. Well, so far so good... Unfortunately, I'm having problems
since when one thread calls "pipe.each_line" the other (!) threads
hang. Well, if I add a "Process.waitpid" call before the call to
pipe.each_line then the external command will sooner or later hang
since noone consumes anything in the pipe.

To exemplify, below are two ruby programs. The first, lotsoflines.rb,
will print so much output that the pipe will be filles (in the real
world this is typically not written in Ruby). The second, test.rb,
without the "waitpid" will block the thread printing "BG Thread"; with
the "waitpid" line the background process will never terminate.

So, what I would need is a way to read from the pipe *without blocking
other threads*, or a wait until there is data to read, or similar. All
suggestions are welcome!

Oh, btw, I need this to work both under win32 and unix.

-- Anders Lindgren

------------------------------------ lotsoflines.rb
ARGV[0].to_i.times do puts "A LINE" end
sleep 2
------------------------------------ test.rb
cmd = "ruby lotsoflines.rb 300"

Thread.new do
while true
sleep 0.1
puts "BG Thread"
end
end

puts "Before popen"

IO.popen(cmd) do |pipe|

puts "Inside popen"

# With this line, the background thread will be blocked, with this
# line the process at the other end of the pipe will hang...

Process.waitpid(pipe.pid)

pipe.each_line do |x|
puts "XXX:" + x
end
end

puts "After popen"

4 Answers

Ara.T.Howard

3/6/2007 2:35:00 PM

0

Anders Lindgren

3/6/2007 3:37:00 PM

0

Thanks for the reply!

> it can't be done using popen and threads with current ruby. search the
> archives.

Well, I did search the archive, but I didn't get a straight answer on
this. I couldn't imagine that it would be impossible to read data from
more than one source at the same time.

You said "current ruby" -- well, maybe there is hope for the future!

I just noticed that in the weekly newsletter it was announced that
Pragmatic Programmers will publish a book on Erlang. This is a
language I worked on many years ago and I haven't really missed it --
until today, that is. The model it uses for building concurrent
applications is really a winner. In that environment you simply
specify the start function (the "main" of the thread), communication
is typically performed by passing messages. The "fork" paradigm and
shared variables used by Ruby is something that really should be
declared as deprecated, since it really makes the program difficult to
write (not to mention impossible to read).

I cross my fingers that the next major version of Ruby uses a non-toy
concurrent solution.


> this may help
>
> http://codeforpeople.com/lib/ruby/systemu/systemu-1....
>
> gem install systemu

Thanks, I will take a look at it!

-- Anders Lindgren

MenTaLguY

3/6/2007 3:50:00 PM

0

On Wed, 7 Mar 2007 00:40:06 +0900, "Anders Lindgren" <andlind@gmail.com> wrote:

> I cross my fingers that the next major version of Ruby uses a non-toy
> concurrent solution.

I'm working on an implementation of actors for Ruby (as part of an omnibus concurrency library). While it doesn't eliminate shared state from the language, in principle it makes it possible to avoid in many cases.

-mental


trotsky

7/2/2012 5:41:00 PM

0

On 7/2/12 9:21 AM, Adam H. Kerman wrote:
> trotsky <gmsingh@email.com> wrote:
>> On 7/1/12 10:21 PM, Adam H. Kerman wrote:
>>> trotsky <gmsingh@email.com> wrote:
>>>> On 7/1/12 6:31 PM, Adam H. Kerman wrote:
>>>>> trotsky <gmsingh@email.com> wrote:
>>>>>> On 7/1/12 11:50 AM, Adam H. Kerman wrote:
>>>>>>> trotsky <gmsingh@email.com> wrote:
>
>>>>>>>> All this bluster from the right and Obama is *still* accomplishing
>>>>>>>> things in office:
>
>>>>>>>> http://firstread.msnbc.msn.com/_news/2012/06/29/12483144-congress-sends-student-loan-and-transportation-package-to-...
>
>>>>>>>>> Congress sends student loan and transportation package to Obama
>
>>>>>>> The highway bill is a complete piece of shit, trots. It fails to
>>>>>>> shore up the highway fund, which has been in deficit for decades,
>>>>>>> and it includes negligible programs for Amtrak and mass transit. By
>>>>>>> continuing to have most transportation spending from general funds,
>>>>>>> we just add to the general deficit. We haven't had decent federal
>>>>>>> transportation programs since Ford.
>
>>>>>> And yet the student loan portion has escaped your razor sharp focus.
>>>>>> You and Thanny have a lot in common!
>
>>>>> I don't know anything about that bill, so I made no comment. You didn't
>>>>> know anything about either bill, so all you did was post a URL.
>
>>>> I see what you're saying--you don't really know if Obama should be given
>>>> credit for accomplishing anything based on the article above, but you
>>>> decided to be contradictory solely so you could act like an asshole.
>>>> Agree or disagree?
>
>>> I don't count getting legislation passed as an accomplishment of the
>>> president or Congress for that matter.
>
>> Really? Republican obstructionism doesn't register in that abscess you
>> call a brain? It sure seems like you're trolling to me.
>
> It's the ordinary job of Congress, you enormous fool.


I see, you don't comprehend what is being discussed. Perhaps you never
heard what Mitch McConnell thinks:

http://www.tnr.com/blog/jonathan-cohn/96591/mcconnell-vill...

His priority is to remove Obama from office, not passing legislation.
Do you care to revise your bullshit story?