Ruby and threads: a VTK example

  • Thread starter Aureliano Buendia
  • Start date
A

Aureliano Buendia

Hi,

There is a ruby wrapper available for vtk which works, more or less,
fine. For those who are not familiar with vtk, it is avisualisation
library written in c++ and the output is a native window showing some
interactive OpenGL graphics.

The VTK code usually ends with:

iren = Vtk::RenderWindowInteractor.new
iren.start()

Here is where the problem comes: iren.start() starts a new thread in
c++, while, I am trying to run the script in irb and interactively
change the objects properties and see the live results in the OpenGL
window. Unfortunately, after iren.start() ruby goes to a deep sleep.
This is the irb output:
?> iren.Start()

which obviously does not let me to enter any more commands unless I
close the window:

=> nil
which is too late for manipulating objects interactively.

It is surprising when a multi-threaded c++ library is single-threaded
after wrapping to ruby. If this does something have to do with ruby not
supporting native threads, perhaps it is a big sacrifice for
portability.

What is the best way to have irb command line, while also having the
graphics rendering window?
 
R

Ross Bamford

There is a ruby wrapper available for vtk which works, more or less,
fine. For those who are not familiar with vtk, it is avisualisation
library written in c++ and the output is a native window showing some
interactive OpenGL graphics.

The VTK code usually ends with:

iren = Vtk::RenderWindowInteractor.new
iren.start()

Here is where the problem comes: iren.start() starts a new thread in
c++, while, I am trying to run the script in irb and interactively
change the objects properties and see the live results in the OpenGL
window. Unfortunately, after iren.start() ruby goes to a deep sleep.
This is the irb output:

?> iren.Start()

which obviously does not let me to enter any more commands unless I
close the window:

=> nil

which is too late for manipulating objects interactively.

I don't have any of this stuff installed, so I'm guessing, but couldn't
you just do e.g.:

winthr = Thread.new { iren.Start() }

// do whatever else you need to do

winthr.kill # or close the window may be better

Obviously I don't know about the concurrency issues but with this being
interactive I'd guess it's not something you'd worry much about.
 
A

Aureliano Buendia

Ross said:
winthr = Thread.new { iren.Start() }

// do whatever else you need to do

winthr.kill # or close the window may be better

I get the same thing:

?> winthr = Thread.new { iren.Start() }

Still no command line. All I want is a command line with access to the
the objects in the running thread.

You do not need to have access to this library, it can be emulated
easily: Assume you want to run a very long process names
VeryLongProcess(). I want a command line when VeryLongProcess() is
running and I want this command line to have acees to all the objects
that VeryLongProcess() has access to them.
 
R

Ross Bamford

I get the same thing:

?> winthr = Thread.new { iren.Start() }

Still no command line. All I want is a command line with access to the
the objects in the running thread.

Well, it looks like something in the library is blocking the calling
process (and all Ruby's green threads) completely. If so, it's probably
something you'd have to talk to the binding's maintainer about.

I did just try to install VTK but it barfed horribly on my Linux box so
I'm afraid I can't be much more help.
You do not need to have access to this library, it can be emulated
easily: Assume you want to run a very long process names
VeryLongProcess(). I want a command line when VeryLongProcess() is
running and I want this command line to have acees to all the objects
that VeryLongProcess() has access to them.

Assuming you mean 'process' in the sense of 'something happening in
Ruby', then the thread thing I suggested should work. If OTOH I'm
missing something and you're referring to actual processes, then it gets
more involved...
 
A

Aureliano Buendia

Ross,

Forget about vtk, put it this way. Save this in a script and run it by
irb:

i = 1
while 1
puts i
end

It doesn't allow you to add any more command lines unless the
never-ending while-loop is over. Wrapping this in a thread does not help
it too:

Thread.new{
i = 1
while 1
puts i
end
}

All I want to do is to have the while loop, and then enter

i = 2

in the command line to get 2 printed.
 
H

Hidetoshi NAGAI

From: Aureliano Buendia <[email protected]>
Subject: Re: Ruby and threads: a VTK example
Date: Fri, 10 Nov 2006 03:21:38 +0900
Message-ID: said:
It doesn't allow you to add any more command lines unless the
never-ending while-loop is over. Wrapping this in a thread does not help
it too:

What is your OS ?
If it is Windows, your trouble may depends on console I/O.
Console I/O of Win (e.g. to get a conmmand line on IRB)
blocks thread-switching of Ruby.
# Please see [ruby-talk:223935] also.
 
A

Aureliano Buendia

Hidetoshi said:
It doesn't allow you to add any more command lines unless the
never-ending while-loop is over. Wrapping this in a thread does not help
it too:

What is your OS ?
If it is Windows, your trouble may depends on console I/O.
Console I/O of Win (e.g. to get a conmmand line on IRB)
blocks thread-switching of Ruby.
# Please see [ruby-talk:223935] also.

Hidetoshi,

The OS is WinXP, I have also tried IRB with eclipse as an external tool
with no luck. What Ross described does not work on windows as he has
ruby on a linux box. The example you mentioned in runy-talk was done
with tk. Is there any ways or doing this in simple dos command prompt
(or eclipse)? I expected ruby to me more portable than this!
 
H

Hidetoshi NAGAI

From: Aureliano Buendia <[email protected]>
Subject: Re: Ruby and threads: a VTK example
Date: Sat, 11 Nov 2006 02:18:18 +0900
Message-ID: said:
The example you mentioned in runy-talk was done
with tk. Is there any ways or doing this in simple dos command prompt
(or eclipse)?

I'm sorry, but I have no idea. If I had, I didn't make irbtkw.rbw.;-)
Probably, it requires rewriting IRB::StdioInputMethod#gets.
I don't know how it has to be changed, because I'm not familiar with
programing on Windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top