Вот решение, которое я нашел, на случай, если оно будет полезным.Обратите внимание - на моем компьютере это существенно не уменьшило время выполнения.
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)