diff options
author | wustho <benawiadha@gmail.com> | 2020-04-24 09:55:24 +0700 |
---|---|---|
committer | wustho <benawiadha@gmail.com> | 2020-04-24 09:55:24 +0700 |
commit | 45c1cdeb6f195b7bba7bd731b6c617e98f4968e7 (patch) | |
tree | 4e528e0350d13496f9e61fafb38def4b1d2381b3 /epy.py | |
parent | 215d1c5ba769bd13e8b89ec708bfe0118fba7dca (diff) | |
download | epy-45c1cdeb6f195b7bba7bd731b6c617e98f4968e7.tar.gz |
Fixed multiprocessing to support single core cpu and termux.
Diffstat (limited to 'epy.py')
-rwxr-xr-x | epy.py | 91 |
1 files changed, 61 insertions, 30 deletions
@@ -14,7 +14,7 @@ Options: """ -__version__ = "2020.4.23" +__version__ = "2020.4.24" __license__ = "MIT" __author__ = "Benawi Adha" __url__ = "https://github.com/wustho/epy" @@ -31,13 +31,13 @@ import json import tempfile import shutil import subprocess +import multiprocessing import xml.etree.ElementTree as ET from urllib.parse import unquote from html import unescape from html.parser import HTMLParser from difflib import SequenceMatcher as SM from functools import wraps -from multiprocessing import Process, Array, Value # -1 is default terminal fg/bg colors @@ -104,12 +104,12 @@ SEARCHPATTERN = None VWR = None DICT = None SCREEN = None -PERCHLETTERS = [] -ALLPREVLETTERS = [] -SUMALLLETTERS = Value("i", 0) -PROC_COUNTLETTERS = None JUMPLIST = {} SHOWPROGRESS = CFG["ShowProgressIndicator"] +MULTIPROC = False if multiprocessing.cpu_count() == 1 else True +ALLPREVLETTERS = [] +SUMALLLETTERS = 0 +PROC_COUNTLETTERS = None class Epub: @@ -656,9 +656,10 @@ def savestate(file, index, width, pos, pctg): with open(STATEFILE, "w") as f: json.dump(STATE, f, indent=4) - # PROC_COUNTLETTERS.terminate() - PROC_COUNTLETTERS.kill() - # PROC_COUNTLETTERS.join() + if MULTIPROC: + # PROC_COUNTLETTERS.terminate() + PROC_COUNTLETTERS.kill() + # PROC_COUNTLETTERS.join() def pgup(pos, winhi, preservedline=0, c=1): @@ -1080,7 +1081,8 @@ def find_curr_toc_id(toc_idx, toc_sect, toc_secid, index, y): return ntoc -def count_pct(ebook, perch, allprev, sumlet): +def count_pct_async(ebook, allprev, sumlet): + perch = [] for n, i in enumerate(ebook.contents): content = ebook.get_raw_text(i) parser = HTMLtoLines() @@ -1090,9 +1092,50 @@ def count_pct(ebook, perch, allprev, sumlet): # except: # pass src_lines = parser.get_lines() - perch[n] = sum([len(re.sub("\s", "", j)) for j in src_lines]) - allprev[n] = sum(perch[:n]) - sumlet.value = sum(perch[:]) + allprev[n] = sum(perch) + perch.append(sum([len(re.sub("\s", "", j)) for j in src_lines])) + sumlet.value = sum(perch) + + +def count_pct(ebook): + perch = [] + allprev = [] + for i in ebook.contents: + content = ebook.get_raw_text(i) + parser = HTMLtoLines() + # try: + parser.feed(content) + parser.close() + # except: + # pass + src_lines = parser.get_lines() + allprev.append(sum(perch)) + perch.append(sum([len(re.sub("\s", "", j)) for j in src_lines])) + sumlet = sum(perch) + return allprev, sumlet + + +def count_max_reading_pg(ebook): + global ALLPREVLETTERS, SUMALLLETTERS, PROC_COUNTLETTERS, MULTIPROC + + if MULTIPROC: + try: + ALLPREVLETTERS = multiprocessing.Array("i", len(ebook.contents)) + SUMALLLETTERS = multiprocessing.Value("i", 0) + PROC_COUNTLETTERS = multiprocessing.Process( + target=count_pct_async, args=( + ebook, + ALLPREVLETTERS, + SUMALLLETTERS + ) + ) + # forking PROC_COUNTLETTERS will raise + # zlib.error: Error -3 while decompressing data: invalid distance too far back + PROC_COUNTLETTERS.start() + except: + MULTIPROC = False + if not MULTIPROC: + ALLPREVLETTERS, SUMALLLETTERS = count_pct(ebook) def reader(ebook, index, width, y, pctg, sect): @@ -1436,8 +1479,9 @@ def reader(ebook, index, width, y, pctg, sect): try: SCREEN.clear() SCREEN.addstr(0, 0, countstring) - if SHOWPROGRESS and (cols-width-2)//2 > 3 and SUMALLLETTERS.value != 0: - PROGRESS = (ALLPREVLETTERS[index] + sum(LOCALPCTG[:y+rows-1])) / SUMALLLETTERS.value + LOCALSUMALLL = SUMALLLETTERS.value if MULTIPROC else SUMALLLETTERS + if SHOWPROGRESS and (cols-width-2)//2 > 3 and LOCALSUMALLL != 0: + PROGRESS = (ALLPREVLETTERS[index] + sum(LOCALPCTG[:y+rows-1])) / LOCALSUMALLL PROGRESSTR = "{}%".format(int(PROGRESS*100)) SCREEN.addstr(0, cols-len(PROGRESSTR), PROGRESSTR) SCREEN.refresh() @@ -1458,7 +1502,7 @@ def reader(ebook, index, width, y, pctg, sect): def preread(stdscr, file): - global COLORSUPPORT, SHOWPROGRESS, PERCHLETTERS, SCREEN, ALLPREVLETTERS, PROC_COUNTLETTERS + global COLORSUPPORT, SHOWPROGRESS, SCREEN curses.use_default_colors() try: @@ -1500,21 +1544,8 @@ def preread(stdscr, file): find_media_viewer() find_dict_client() parse_keys() - - PERCHLETTERS = Array("i", len(ebook.contents)) - ALLPREVLETTERS = Array("i", len(ebook.contents)) SHOWPROGRESS = CFG["ShowProgressIndicator"] - PROC_COUNTLETTERS = Process( - target=count_pct, args=( - ebook, - PERCHLETTERS, - ALLPREVLETTERS, - SUMALLLETTERS - ) - ) - # forking PROC_COUNTLETTERS will raise - # zlib.error: Error -3 while decompressing data: invalid distance too far back - PROC_COUNTLETTERS.start() + count_max_reading_pg(ebook) sec = "" while True: |