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