You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Executes python scripts in different environments without writing temp files to disk, it can pass and receive any variable, multiprocessing can be executed from everywhere, not only at top-level
pip install callpyfile
# Create a file called callc1.pyimportsysfromcallpyfileimportrun_py_file# use this function to call the pyfilefroma_cv_imwrite_imread_plusimportopen_image_in_cv# an example with OpenCVpics= [
"https://github.com/hansalemaos/screenshots/raw/main/gimages/elephant.png",
"https://github.com/hansalemaos/screenshots/raw/main/gimages/dl.png",
"https://github.com/hansalemaos/screenshots/raw/main/gimages/house.png",
]
allpics= []
allpics_= [open_image_in_cv(x) forxinpics]
forpinrange(100):
allpics.extend(allpics_)
# Execute the py fileresu=run_py_file(
variables={
"imagelist": allpics
}, # The keys will be the global variables in the called pyfilepyfile=r"C:\Users\Gamer\anaconda3\envs\dfdir\callc2.py", # The pyfileactivate_env_command="", # if you want to activate another environment, you can pass something like this (Anaconda): activate_env_command = ['activate.bat', 'otherenv']pythonexe=sys.executable, # python.exe from your active environmentraise_exception=False, # if raise_exception is False, the function returns None if an Exception occurs during the Execution
)
# Create another file called callc2.pyimporttimeimportcv2frompathos.multiprocessingimportProcessingPoolasPool# By the way: good stuffimportnumpyasnpfromcallpyfileimportto_stdout# import the decorator# Some CV stuffdefdo_cv2_stuff(img):
kernel=np.ones((5, 5), np.float32) /25dst=cv2.filter2D(img, -1, kernel)
returndst@to_stdout(kill_if_exception=True) # optional, kills the process using os._exit()defexemu():
pool=Pool(nodes=5)
res=pool.amap(
do_cv2_stuff, imagelist
) # imagelist is not defined yet, but it doesn't matter because the decorator will create global variables from the dict you'have passed: {"imagelist": allpics},whilenotres.ready():
time.sleep(0.001)
allb=res.get()
returnallbif__name__=="__main__":
exemu()