mirror of
https://github.com/ciromattia/kcc
synced 2025-12-23 14:41:47 +00:00
Refactored multiprocessing support
This commit is contained in:
@@ -31,7 +31,7 @@ import stat
|
|||||||
import string
|
import string
|
||||||
from shutil import move, copyfile, copytree, rmtree, make_archive
|
from shutil import move, copyfile, copytree, rmtree, make_archive
|
||||||
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:
|
||||||
from PyQt4 import QtCore
|
from PyQt4 import QtCore
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -308,68 +308,54 @@ def applyImgOptimization(img, opt, overrideQuality=5):
|
|||||||
|
|
||||||
|
|
||||||
def dirImgProcess(path):
|
def dirImgProcess(path):
|
||||||
|
global workerPool, workerOutput
|
||||||
|
workerPool = Pool()
|
||||||
|
workerOutput = []
|
||||||
work = []
|
work = []
|
||||||
pagenumber = 0
|
pagenumber = 0
|
||||||
pagenumbermodifier = 0
|
|
||||||
queue = Queue()
|
|
||||||
pool = Pool(None, fileImgProcess_init, [queue, options])
|
|
||||||
for (dirpath, dirnames, filenames) in os.walk(path):
|
for (dirpath, dirnames, filenames) in os.walk(path):
|
||||||
for afile in filenames:
|
for afile in filenames:
|
||||||
if getImageFileName(afile) is not None:
|
if getImageFileName(afile) is not None:
|
||||||
pagenumber += 1
|
pagenumber += 1
|
||||||
work.append([afile, dirpath, pagenumber])
|
work.append([afile, dirpath, options])
|
||||||
if GUI:
|
if GUI:
|
||||||
GUI.emit(QtCore.SIGNAL("progressBarTick"), pagenumber)
|
GUI.emit(QtCore.SIGNAL("progressBarTick"), pagenumber)
|
||||||
if len(work) > 0:
|
if len(work) > 0:
|
||||||
splitpages = pool.map_async(func=fileImgProcess, iterable=work)
|
for i in work:
|
||||||
pool.close()
|
workerPool.apply_async(func=fileImgProcess, args=(i, ), callback=fileImgProcess_tick)
|
||||||
if GUI:
|
workerPool.close()
|
||||||
while not splitpages.ready():
|
workerPool.join()
|
||||||
# noinspection PyBroadException
|
if GUI and not GUI.conversionAlive:
|
||||||
try:
|
|
||||||
queue.get(True, 5)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
if not GUI.conversionAlive:
|
|
||||||
pool.terminate()
|
|
||||||
rmtree(os.path.join(path, '..', '..'), True)
|
rmtree(os.path.join(path, '..', '..'), True)
|
||||||
raise UserWarning("Conversion interrupted.")
|
raise UserWarning("Conversion interrupted.")
|
||||||
GUI.emit(QtCore.SIGNAL("progressBarTick"))
|
if len(workerOutput) > 0:
|
||||||
pool.join()
|
|
||||||
queue.close()
|
|
||||||
try:
|
|
||||||
splitpages = splitpages.get()
|
|
||||||
except:
|
|
||||||
rmtree(os.path.join(path, '..', '..'), True)
|
rmtree(os.path.join(path, '..', '..'), True)
|
||||||
raise RuntimeError("One of workers crashed. Cause: " + str(sys.exc_info()[1]))
|
raise RuntimeError("One of workers crashed. Cause: " + workerOutput[0])
|
||||||
splitpages = filter(None, splitpages)
|
|
||||||
splitpages.sort()
|
|
||||||
for page in splitpages:
|
|
||||||
if (page + pagenumbermodifier) % 2 == 0:
|
|
||||||
pagenumbermodifier += 1
|
|
||||||
pagenumbermodifier += 1
|
|
||||||
else:
|
else:
|
||||||
rmtree(os.path.join(path, '..', '..'), True)
|
rmtree(os.path.join(path, '..', '..'), True)
|
||||||
raise UserWarning("Source directory is empty.")
|
raise UserWarning("Source directory is empty.")
|
||||||
|
|
||||||
|
|
||||||
def fileImgProcess_init(queue, opt):
|
def fileImgProcess_tick(output):
|
||||||
fileImgProcess.queue = queue
|
if output:
|
||||||
fileImgProcess.options = opt
|
workerOutput.append(output)
|
||||||
|
workerPool.terminate()
|
||||||
|
if GUI:
|
||||||
|
GUI.emit(QtCore.SIGNAL("progressBarTick"))
|
||||||
|
if not GUI.conversionAlive:
|
||||||
|
workerPool.terminate()
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyUnresolvedReferences
|
|
||||||
def fileImgProcess(work):
|
def fileImgProcess(work):
|
||||||
|
#noinspection PyBroadException
|
||||||
|
try:
|
||||||
afile = work[0]
|
afile = work[0]
|
||||||
dirpath = work[1]
|
dirpath = work[1]
|
||||||
pagenumber = work[2]
|
opt = work[2]
|
||||||
opt = fileImgProcess.options
|
|
||||||
output = None
|
|
||||||
if opt.verbose:
|
if opt.verbose:
|
||||||
print "Optimizing " + afile + " for " + opt.profile
|
print "Optimizing " + afile + " for " + opt.profile
|
||||||
else:
|
else:
|
||||||
print ".",
|
print ".",
|
||||||
fileImgProcess.queue.put(".")
|
|
||||||
img = image.ComicPage(os.path.join(dirpath, afile), opt.profileData)
|
img = image.ComicPage(os.path.join(dirpath, afile), opt.profileData)
|
||||||
if opt.quality == 2:
|
if opt.quality == 2:
|
||||||
wipe = False
|
wipe = False
|
||||||
@@ -382,7 +368,6 @@ def fileImgProcess(work):
|
|||||||
if split is not None:
|
if split is not None:
|
||||||
if opt.verbose:
|
if opt.verbose:
|
||||||
print "Splitted " + afile
|
print "Splitted " + afile
|
||||||
output = pagenumber
|
|
||||||
img0 = image.ComicPage(split[0], opt.profileData)
|
img0 = image.ComicPage(split[0], opt.profileData)
|
||||||
applyImgOptimization(img0, opt)
|
applyImgOptimization(img0, opt)
|
||||||
img0.saveToDir(dirpath, opt.forcepng, opt.forcecolor, wipe)
|
img0.saveToDir(dirpath, opt.forcepng, opt.forcecolor, wipe)
|
||||||
@@ -406,7 +391,8 @@ def fileImgProcess(work):
|
|||||||
img2.rotated = True
|
img2.rotated = True
|
||||||
applyImgOptimization(img2, opt, 0)
|
applyImgOptimization(img2, opt, 0)
|
||||||
img2.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True)
|
img2.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True)
|
||||||
return output
|
except:
|
||||||
|
return str(sys.exc_info()[1])
|
||||||
|
|
||||||
|
|
||||||
def genEpubStruct(path):
|
def genEpubStruct(path):
|
||||||
|
|||||||
Reference in New Issue
Block a user