# Enables multiprocessing with caching, eliminating the need for __main__, and ensuring unique results without duplicates
## pip install multiprocca
## Tested against Python 3.11 / Windows 10
```python
import sys
from a_cv_imwrite_imread_plus import open_image_in_cv
from locate_pixelcolor_c import search_colors
import subprocess
import numpy as np
import cv2
from list_all_files_recursively import get_folder_file_complete_path
from multiprocca import start_multiprocessing
from multiprocca.proclauncher import MultiProcExecution
def function_subxxx(x):
exec("import cv2", globals())
exec("import numpy as np", globals())
pic = cv2.imread(x)
b, g, r = pic[..., 0], pic[..., 1], pic[..., 2]
return np.where(((b == 255) & (g == 255) & (r == 255)))
def function_sub1(file):
exec("import subprocess", globals())
exec("import numpy as np", globals())
with open(file, mode="rb") as f:
da = f.read()
p = subprocess.run(
[
r"""C:\Program Files\Tesseract-OCR\tesseract.exe""",
"-c",
"tessedit_create_tsv=1",
"-l",
"por",
"-",
"stdout",
],
input=da,
capture_output=True,
)
return p.stderr, p.stdout
def function_sub(pic, colors):
try:
exec("""import sys""", globals())
exec("""import numpy as np""", globals())
exec("""import cv2""", globals())
exec("""from locate_pixelcolor_c import search_colors""", globals())
exec("""from a_cv_imwrite_imread_plus import open_image_in_cv""", globals())
colorsrev = np.array([list(q) for q in (map(reversed, colors))], dtype=np.uint8)
pic = open_image_in_cv(pic, channels_in_output=3)
sc = search_colors(pic=pic, colors=colorsrev)
if len(sc) == 1:
if np.sum(sc) == 0:
if not (pic[0, 0]) in colorsrev:
sc = np.array([[-1, -1]], dtype=np.int32)
return sc
except Exception as e:
sys.stderr.write(f"{e}\n")
sys.stderr.flush()
return np.array([[-1, -1]], dtype=np.int32)
colors1 = (
(69, 71, 66),
(255, 255, 255),
(153, 155, 144),
(55, 57, 52),
(136, 138, 127),
(56, 58, 53),
(54, 56, 51),
(0, 180, 252),
)
allpi = [
x.path
for x in get_folder_file_complete_path(r"C:\testfolderall")
if x.ext == ".png"
]
f = [
MultiProcExecution(fu=function_sub, args=(o, colors1), kwargstuple=())
for o in allpi
]
formated_data, raw_data = start_multiprocessing(
it=f,
usecache=True,
processes=5,
chunks=1,
print_stdout=False,
print_stderr=False,
)
def start_multiprocessing(
it,
usecache=True,
processes=5,
chunks=1,
print_stdout=False,
print_stderr=True,
):
r"""
Initiates parallel processing on the given iterable using multiprocessing.
Args:
it (iterable): The iterable containing data to be processed in parallel.
usecache (bool, optional): Flag indicating whether to enable caching of results.
Defaults to True.
processes (int, optional): The number of parallel processes to be spawned.
Defaults to 5.
chunks (int, optional): The number of items to be processed in each task chunk.
Defaults to 1.
print_stdout (bool, optional): Flag indicating whether to print stdout of subprocesses.
Defaults to False.
print_stderr (bool, optional): Flag indicating whether to print stderr of subprocesses.
Defaults to True.
Returns:
tuple: A tuple containing two elements:
1. A dictionary mapping input indices to corresponding processed results.
2. A list containing essential data, including hash_and_result, hash_int_map_small,
original_object, and mapping_dict.
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/multiprocca",
"name": "multiprocca",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "multiprocessing,cpus",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/06/f6/4b63a45133823b05a8b185261476bd0641eb6ddcc2a1f2bc0987594f26b6/multiprocca-0.10.tar.gz",
"platform": null,
"description": "\r\n# Enables multiprocessing with caching, eliminating the need for __main__, and ensuring unique results without duplicates\r\n\r\n## pip install multiprocca\r\n\r\n\r\n## Tested against Python 3.11 / Windows 10\r\n\r\n\r\n```python\r\nimport sys\r\nfrom a_cv_imwrite_imread_plus import open_image_in_cv\r\nfrom locate_pixelcolor_c import search_colors\r\nimport subprocess\r\nimport numpy as np\r\nimport cv2\r\nfrom list_all_files_recursively import get_folder_file_complete_path\r\nfrom multiprocca import start_multiprocessing\r\nfrom multiprocca.proclauncher import MultiProcExecution \r\n\r\ndef function_subxxx(x):\r\n exec(\"import cv2\", globals())\r\n exec(\"import numpy as np\", globals())\r\n\r\n pic = cv2.imread(x)\r\n b, g, r = pic[..., 0], pic[..., 1], pic[..., 2]\r\n return np.where(((b == 255) & (g == 255) & (r == 255)))\r\n\r\n\r\ndef function_sub1(file):\r\n exec(\"import subprocess\", globals())\r\n exec(\"import numpy as np\", globals())\r\n\r\n with open(file, mode=\"rb\") as f:\r\n da = f.read()\r\n p = subprocess.run(\r\n [\r\n r\"\"\"C:\\Program Files\\Tesseract-OCR\\tesseract.exe\"\"\",\r\n \"-c\",\r\n \"tessedit_create_tsv=1\",\r\n \"-l\",\r\n \"por\",\r\n \"-\",\r\n \"stdout\",\r\n ],\r\n input=da,\r\n capture_output=True,\r\n )\r\n return p.stderr, p.stdout\r\n\r\n\r\ndef function_sub(pic, colors):\r\n try:\r\n exec(\"\"\"import sys\"\"\", globals())\r\n exec(\"\"\"import numpy as np\"\"\", globals())\r\n exec(\"\"\"import cv2\"\"\", globals())\r\n exec(\"\"\"from locate_pixelcolor_c import search_colors\"\"\", globals())\r\n exec(\"\"\"from a_cv_imwrite_imread_plus import open_image_in_cv\"\"\", globals())\r\n colorsrev = np.array([list(q) for q in (map(reversed, colors))], dtype=np.uint8)\r\n\r\n pic = open_image_in_cv(pic, channels_in_output=3)\r\n sc = search_colors(pic=pic, colors=colorsrev)\r\n if len(sc) == 1:\r\n if np.sum(sc) == 0:\r\n if not (pic[0, 0]) in colorsrev:\r\n sc = np.array([[-1, -1]], dtype=np.int32)\r\n return sc\r\n except Exception as e:\r\n sys.stderr.write(f\"{e}\\n\")\r\n sys.stderr.flush()\r\n return np.array([[-1, -1]], dtype=np.int32)\r\n\r\n\r\ncolors1 = (\r\n (69, 71, 66),\r\n (255, 255, 255),\r\n (153, 155, 144),\r\n (55, 57, 52),\r\n (136, 138, 127),\r\n (56, 58, 53),\r\n (54, 56, 51),\r\n (0, 180, 252),\r\n)\r\nallpi = [\r\n x.path\r\n for x in get_folder_file_complete_path(r\"C:\\testfolderall\")\r\n if x.ext == \".png\"\r\n]\r\nf = [\r\n MultiProcExecution(fu=function_sub, args=(o, colors1), kwargstuple=())\r\n for o in allpi\r\n]\r\nformated_data, raw_data = start_multiprocessing(\r\n it=f,\r\n usecache=True,\r\n processes=5,\r\n chunks=1,\r\n print_stdout=False,\r\n print_stderr=False,\r\n)\r\n\r\n\r\ndef start_multiprocessing(\r\n it,\r\n usecache=True,\r\n processes=5,\r\n chunks=1,\r\n print_stdout=False,\r\n print_stderr=True,\r\n):\r\n r\"\"\"\r\n Initiates parallel processing on the given iterable using multiprocessing.\r\n\r\n Args:\r\n it (iterable): The iterable containing data to be processed in parallel.\r\n usecache (bool, optional): Flag indicating whether to enable caching of results.\r\n Defaults to True.\r\n processes (int, optional): The number of parallel processes to be spawned.\r\n Defaults to 5.\r\n chunks (int, optional): The number of items to be processed in each task chunk.\r\n Defaults to 1.\r\n print_stdout (bool, optional): Flag indicating whether to print stdout of subprocesses.\r\n Defaults to False.\r\n print_stderr (bool, optional): Flag indicating whether to print stderr of subprocesses.\r\n Defaults to True.\r\n\r\n Returns:\r\n tuple: A tuple containing two elements:\r\n 1. A dictionary mapping input indices to corresponding processed results.\r\n 2. A list containing essential data, including hash_and_result, hash_int_map_small,\r\n original_object, and mapping_dict.\r\n\r\n\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Enables multiprocessing with caching, eliminating the need for __main__, and ensuring unique results without duplicates",
"version": "0.10",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/multiprocca"
},
"split_keywords": [
"multiprocessing",
"cpus"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "15570a1c137c2792d43dd7e579839ba2ea0be5da990d545964f895f169092bdc",
"md5": "9f3258ddc41ab5f343bad67a804e97c5",
"sha256": "6e2949b2f764f9bcf3bdc1bb6d63abaa2ba4f51cd93c394d50df03c89f035264"
},
"downloads": -1,
"filename": "multiprocca-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9f3258ddc41ab5f343bad67a804e97c5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 9100,
"upload_time": "2023-11-11T23:40:21",
"upload_time_iso_8601": "2023-11-11T23:40:21.920407Z",
"url": "https://files.pythonhosted.org/packages/15/57/0a1c137c2792d43dd7e579839ba2ea0be5da990d545964f895f169092bdc/multiprocca-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "06f64b63a45133823b05a8b185261476bd0641eb6ddcc2a1f2bc0987594f26b6",
"md5": "4780de98d24133b8766c60e6fcb83e0e",
"sha256": "3baed48091e785f0d1700bc5998d3098795f76d7a8d0deab1069040b0a0a04a3"
},
"downloads": -1,
"filename": "multiprocca-0.10.tar.gz",
"has_sig": false,
"md5_digest": "4780de98d24133b8766c60e6fcb83e0e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7125,
"upload_time": "2023-11-11T23:40:23",
"upload_time_iso_8601": "2023-11-11T23:40:23.695416Z",
"url": "https://files.pythonhosted.org/packages/06/f6/4b63a45133823b05a8b185261476bd0641eb6ddcc2a1f2bc0987594f26b6/multiprocca-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-11 23:40:23",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "multiprocca",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "multiprocca"
}