Discussion:
[vtkusers] Threading with vtkRenderWidget and Python?
Utsav Pardasani
2003-01-07 17:46:26 UTC
Permalink
Hello friends. I would like to make a multithreaded application with VTK and python. Is it able
to work? I attempted to do a simple example and it is unable to execute. Can somebody verify it?

from Tkinter import *
from vtkpython import *
import thread
from vtkRenderWidget import vtkTkRenderWidget
root = Tk()
def function():
renderWidget = vtkTkRenderWidget(root,width=400,height=400)
renderWidget.pack(expand='true',fill='both')
renWin = renderWidget.GetRenderWindow()
ren = vtkRenderer()
renWin.AddRenderer(ren)
cone = vtkCylinderSource()
cone.SetRadius(0.5)
cone.SetHeight(10)
cone.SetResolution(16)
coneMapper = vtkPolyDataMapper()
coneMapper.SetInput(cone.GetOutput())
coneActor = vtkActor()
coneActor.SetMapper(coneMapper)
ren.AddActor(coneActor)

root.mainloop()
thread.start_new_thread(function,())

Many Thanks,
Utsav

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
Prabhu Ramachandran
2003-01-08 08:43:48 UTC
Permalink
hi,
UP> Hello friends. I would like to make a multithreaded
UP> application with VTK and python. Is it able to work? I
UP> attempted to do a simple example and it is unable to
UP> execute. Can somebody verify it?

[snip]

The way you have implemented the threading is that Tk's mainloop is
running as the main thread and you are then invoking vtk related
things in a separate thread. This will usually lead to disastrous
results the best case being you will see nothing on screen and the
worst case being your X session will crash.

The way I find it convenient to do things with Python, multiple
threads and VTK is to put Tk and VTK into one thread (the main thread
of execution -- we'll call it main) and then do your application
specific stuff (like computations etc. -- lets call it thread1) in a
separate thread. Then to invoke a function from thread1 in main you
make sure that main executes the function by asking Tkinter to make
the call. Something like so:

root = Tk()

# now from your thread1 do the following:
root.after(1, function, args)

Where function is the VTK/Tkinter specific function you want to call
from thread1. Alternatively, add a function thats part of your main
thread of execution to invoke the function for you. root.after is
just a convenient way of doing that from Tkinter. Basically you have
to ensure that all the GUI/VTK commands originate from the same thread
that they (GUI and VTK) are running in.

This should work reliably.

cheers,
prabhu

Loading...