[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby and threads: a VTK example

Aureliano Buendia

11/9/2006 5:09:00 PM

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?

--
Posted via http://www.ruby-....

8 Answers

Ross Bamford

11/9/2006 5:25:00 PM

0

On Fri, 2006-11-10 at 02:09 +0900, Aureliano Buendia wrote:
> 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.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk


Aureliano Buendia

11/9/2006 5:36:00 PM

0

Ross Bamford wrote:

> 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.

--
Posted via http://www.ruby-....

Ross Bamford

11/9/2006 6:08:00 PM

0

On Fri, 2006-11-10 at 02:36 +0900, Aureliano Buendia wrote:
> Ross Bamford wrote:
>
> > 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.
>

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...

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk


Aureliano Buendia

11/9/2006 6:22:00 PM

0

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.

--
Posted via http://www.ruby-....

Ross Bamford

11/9/2006 6:48:00 PM

0

On Thu, 09 Nov 2006 18:21:38 -0000, Aureliano Buendia <saveez@hotmail.co=
m> =

wrote:

> Ross,
>
> Forget about vtk, put it this way. Save this in a script and run it by=

> irb:
>
> i =3D 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.

True.

> Wrapping this in a thread does not help it too:
>
> Thread.new{
> i =3D 1
> while 1
> puts i
> end
> }
>

Apart from the fact that the output makes it difficult to type, that wor=
ks =

fine for me, with one small change - you need to declare 'i' before you =
=

start the thread - otherwise, it'll be local to the block you run in the=
=

thread. So try (rubified a bit):

i =3D 1

Thread.new { loop { puts i } }

You'll get lots of '1's printed out. If you just jump in and type 'i =3D=
2' =

and hit return, the '1's become '2's. Does that not work for you?

-- =

Ross Bamford - rosco@roscopeco.remove.co.uk

Hidetoshi NAGAI

11/10/2006 2:05:00 PM

0

Aureliano Buendia

11/10/2006 5:18:00 PM

0

Hidetoshi NAGAI wrote:

>> 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!

--
Posted via http://www.ruby-....

Hidetoshi NAGAI

11/11/2006 2:11:00 PM

0