Difference between revisions of "2013 Project Week:Threaded SimpleITK Modules"

From NAMIC Wiki
Jump to: navigation, search
(Created page with '__NOTOC__ <gallery> Image:PW-SLC2013.png|Projects List Image:yourimagehere.png| Image description </gallery> ==Key Investigators== * Brad …')
 
Line 34: Line 34:
 
== Preliminary Work ==
 
== Preliminary Work ==
  
The example code below calls two different python functions from inside threads.  One function calls the 'time.sleep' method, and one uses VTK to do some processing.  As you can see from the output printed below, the VTK threads block, and a new thread is not started until the last one completes.  On the other hand, the sleep based thread does return right away.  The behavior is based on the [http://docs.python.org/2/glossary.html#term-global-interpreter-lock CPython global interpreter lock].
+
The example code below calls two different python functions from inside threads.  One function calls the 'time.sleep' method, and one uses VTK to do some processing.  As you can see from the output printed below, the VTK threads block, and a new thread is not started until the last one completes.  On the other hand, the sleep based thread does return right away.  The behavior is based on the [http://docs.python.org/2/glossary.html#term-global-interpreter-lock CPython global interpreter lock]. The topic was [http://web.archiveorange.com/archive/v/aysypJwvM9H8MY8BtpyP discussed in detail] on the vtk developers list back in 2005.
 +
 
  
 
<pre>
 
<pre>

Revision as of 22:26, 18 December 2012

Home < 2013 Project Week:Threaded SimpleITK Modules

Key Investigators

  • Brad Lowekamp, NLM
  • Steve Pieper, Isomics

Project Description

Objective

  • Support multi-threaded execution of SimpleITK filters.

Approach, Plan

Progress

  • Progress here

Preliminary Work

The example code below calls two different python functions from inside threads. One function calls the 'time.sleep' method, and one uses VTK to do some processing. As you can see from the output printed below, the VTK threads block, and a new thread is not started until the last one completes. On the other hand, the sleep based thread does return right away. The behavior is based on the CPython global interpreter lock. The topic was discussed in detail on the vtk developers list back in 2005.


import vtk
import threading
import Queue
import time, datetime
import os

class ThreadClass(threading.Thread):
  def __init__(self, queue, work):
    threading.Thread.__init__(self)
    self.queue = queue
    self.work = work
          
  def run(self):
    now = datetime.datetime.now()
    self.queue.put( "start %s, time: %s" % (self.getName(), now) )
    self.work()
    now = datetime.datetime.now()
    self.queue.put( "end %s, time: %s" % (self.getName(), now) )

def process():
  e = vtk.vtkImageEllipsoidSource()
  e.SetWholeExtent(0,300,0,300,0,300)
  s = vtk.vtkImageGaussianSmooth()
  s.SetInputConnection(e.GetOutputPort())
  s.Update()

def sleep():
  time.sleep(1)

for work,message in ((process,'vtk'), (sleep,'sleep')):
  print ('threading test with %s:' % message)
  threads = 3
  queue = Queue.Queue()
  for i in range(threads):
    t = ThreadClass(queue,work)
    t.start()
  print('started:')

  for i in range(2*threads):
    print(queue.get())
  print('finished\n')


#8 Slicer-superbuild $ ./VTK-build/bin/vtkpython /Users/pieper/Dropbox/hacks/threadVTK.py
threading test with vtk:
started:
start Thread-1, time: 2012-12-18 17:13:17.047282
end Thread-1, time: 2012-12-18 17:13:18.006989
start Thread-2, time: 2012-12-18 17:13:18.007318
end Thread-2, time: 2012-12-18 17:13:18.953446
start Thread-3, time: 2012-12-18 17:13:18.953762
end Thread-3, time: 2012-12-18 17:13:19.897673
finished

threading test with sleep:
started:
start Thread-4, time: 2012-12-18 17:13:19.898124
start Thread-5, time: 2012-12-18 17:13:19.898346
start Thread-6, time: 2012-12-18 17:13:19.898506
end Thread-4, time: 2012-12-18 17:13:20.899137
end Thread-6, time: 2012-12-18 17:13:20.899378
end Thread-5, time: 2012-12-18 17:13:20.899469
finished