1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-13 09:46:25 +00:00

Comic2Panel: Refactored multiprocessing support

This commit is contained in:
Paweł Jastrzębski
2013-10-18 10:37:35 +02:00
parent 5e8bd52433
commit eedf537902

View File

@@ -27,7 +27,7 @@ import os
import sys
from shutil import rmtree, copytree, move
from optparse import OptionParser, OptionGroup
from multiprocessing import Pool, Queue, freeze_support
from multiprocessing import Pool, freeze_support
try:
# noinspection PyUnresolvedReferences
from PIL import Image, ImageStat
@@ -83,21 +83,26 @@ def sanitizePanelSize(panel, opt):
return newPanels
def splitImage_init(queue, opt):
splitImage.queue = queue
splitImage.options = opt
def splitImage_tick(output):
if output:
splitWorkerOutput.append(output)
splitWorkerPool.terminate()
if GUI:
GUI.emit(QtCore.SIGNAL("progressBarTick"))
if not GUI.conversionAlive:
splitWorkerPool.terminate()
# noinspection PyUnresolvedReferences
def splitImage(work):
#noinspection PyBroadException
try:
path = work[0]
name = work[1]
opt = splitImage.options
opt = work[2]
# Harcoded opttions
threshold = 1.0
delta = 15
print ".",
splitImage.queue.put(".")
fileExpanded = os.path.splitext(name)
filePath = os.path.join(path, name)
# Detect corrupted files
@@ -190,6 +195,8 @@ def splitImage(work):
str(pageNumber) + '.png'), 'PNG')
pageNumber += 1
os.remove(filePath)
except:
return str(sys.exc_info()[1])
def Copyright():
@@ -199,7 +206,7 @@ def Copyright():
# noinspection PyBroadException
def main(argv=None, qtGUI=None):
global options
global options, GUI, splitWorkerPool, splitWorkerOutput
parser = OptionParser(usage="Usage: %prog [options] comic_folder", add_help_option=False)
mainOptions = OptionGroup(parser, "MANDATORY")
otherOptions = OptionGroup(parser, "OTHER")
@@ -230,37 +237,28 @@ def main(argv=None, qtGUI=None):
copytree(options.sourceDir, options.targetDir)
work = []
pagenumber = 0
queue = Queue()
pool = Pool(None, splitImage_init, [queue, options])
splitWorkerOutput = []
splitWorkerPool = Pool()
for root, dirs, files in os.walk(options.targetDir, False):
for name in files:
if getImageFileName(name) is not None:
pagenumber += 1
work.append([root, name])
work.append([root, name, options])
else:
os.remove(os.path.join(root, name))
if GUI:
GUI.emit(QtCore.SIGNAL("progressBarTick"), pagenumber)
if len(work) > 0:
workers = pool.map_async(func=splitImage, iterable=work)
pool.close()
if GUI:
while not workers.ready():
# noinspection PyBroadException
try:
queue.get(True, 5)
except:
pass
GUI.emit(QtCore.SIGNAL("progressBarTick"))
pool.join()
queue.close()
try:
workers.get()
except:
for i in work:
splitWorkerPool.apply_async(func=splitImage, args=(i, ), callback=splitImage_tick)
splitWorkerPool.close()
splitWorkerPool.join()
if GUI and not GUI.conversionAlive:
rmtree(options.targetDir, True)
raise RuntimeError("One of workers crashed. Cause: " + str(sys.exc_info()[1]))
if GUI:
GUI.emit(QtCore.SIGNAL("progressBarTick"), 1)
raise UserWarning("Conversion interrupted.")
if len(splitWorkerOutput) > 0:
rmtree(options.targetDir, True)
raise RuntimeError("One of workers crashed. Cause: " + splitWorkerOutput[0])
if options.inPlace:
rmtree(options.sourceDir)
move(options.targetDir, options.sourceDir)