I have written an image analysis pipeline using Jython in ImageJ. I am interested in using multithreading to speed up the process. Basically the pipeline processes multiple images (in the same way) and I would like to concurrently process the images. I have seen an example using Python and multiprocessing (Running python multiprocess for Image processing). This is not possible with Jython. Any help on how to proceed would be much appreciated (I am completely new to multithreading)
Asked
Active
Viewed 559 times
2 Answers
1
The Definitive Guide to Jython book has a chapter on concurrency, which provides a very thorough answer to this question:

ctrueden
- 6,751
- 3
- 37
- 69
0
Here is the solution i found, in case it is helpful. Note - on my computer this did not significantly decrease the execution time.
from java.util.concurrent import Callable
from java.util.concurrent import Executors, TimeUnit
# get user to select a directory and list all file names
settings = IJ.getDirectory("Choose a Directory")
for dirname, dirnames, filenames in os.walk(settings):
for filename in filenames:
SITES.append(os.path.join(dirname, filename))
# function for shutting down the pool - taken from:
# http://www.jython.org/jythonbook/en/1.0/Concurrency.html
def shutdown_and_await_termination(pool, timeout):
pool.shutdown()
try:
if not pool.awaitTermination(timeout, TimeUnit.SECONDS):
pool.shutdownNow()
if (not pool.awaitTermination(timeout, TimeUnit.SECONDS)):
print >> sys.stderr, "Pool did not terminate"
except InterruptedException, ex:
# (Re-)Cancel if current thread also interrupted
pool.shutdownNow()
# Preserve interrupt status
Thread.currentThread().interrupt()
# function that will change all images listed - in this case convert to 8bit
def help(imp):
conv = ImageConverter(imp)
conv.convertToGray8()
# make a callable interface where image will be each image in the list
# in this case each image will be opened, the help function run, which
# converts to 8bit and then the image is displayed on screen
class imageConverter(Callable):
def __init__(self):
self.test = image
def call(self):
imp = IJ.openImage(self.test)
help(imp)
imp.show()
# define the number of threads
MAX_CONCURRENT = 2
pool = Executors.newFixedThreadPool(MAX_CONCURRENT)
# define the task to do in a multithreaded way
imageConverter = [imageConverter() for image in SITES]
# use all defined threads to convert the images
pool.invokeAll(imageConverter)
shutdown_and_await_termination(pool, 5)

mikew
- 31
- 7