Q : "if n_jobs
is more, than joblib
does not call the function."
Well, it does ( you may check the PID & PPID numbers ),
it just does not present the result of the print( "here" )
Using the definition from API documentation :
print( *objects, sep = ' ', end = '\n', file = sys.stdout, flush = False )
start with enforcing the flush = True
Yet, going to face more troubles down the road. joblib
-spawns ( unless forced otherwise, at a cost of adverse effects on performance, if returning back to a pure-[SERIAL]
, GIL-controlled re-[SERIAL]
-ised code execution for any n_jobs
running again one-step-after-another-after-another, which does not make sense, as you pay all the costs of instantiations and other overheads, yet not receiving any speed-up benefit from doing so, does it? ). Using:
def iter_preDEMO( data, # Pandas DF-alike data
#other args removed for MCVE-clarity
):
def fit_by_idx( idx ): #-------------------------------------[FUNCTION]-def-<start> To be transferred to each remote-joblib-initiated process(es)
print( 'here[{0:_>4d}(PPID:PID={1:_>7d}:{2::>7d})]'.format( idx,
os.getppid(), # test joblib-[FUNCTION]-def-transfer here with: lambda x = "_{0:}_" : x.format( os.getppid() )
os.getpid() # test joblib-[FUNCTION]-def-transfer here with: lambda x = "_{0:}_" : x.format( os.getpid() )
),
end = "\t",
flush = True
)
#------------------------------------------------------------[FUNCTION]-def-<end>
res = np.zeros( ( data.shape[0], 3 ) )
for aBackEND in ( 'threading', 'loky', 'multiprocessing' ):
try:
print( "\n____________________________Going into ['{0:}']-backend".format( aBackEND ) )
with parallel_backend( aBackEND, n_jobs = N_JOBS ):
Parallel( n_jobs = N_JOBS )( delayed( fit_by_idx )( pickled_SER_DES_copy_of_idx )
for pickled_SER_DES_copy_of_idx in range( data.shape[0] )
)
finally:
print( "\n_____________________________Exit from ['{0:}']-backend".format( aBackEND ) )
return res
you shall see how the things work, using a bit more detailed, print()
-ed results
START: PID=_____22528
____________________________Going into ['threading']-backend
here[___0(PPID:PID=__22527:::22528)] here[___1(PPID:PID=__22527:::22528)] here[___2(PPID:PID=__22527:::22528)] here[___3(PPID:PID=__22527:::22528)] here[___4(PPID:PID=__22527:::22528)] here[___5(PPID:PID=__22527:::22528)] here[___6(PPID:PID=__22527:::22528)] here[___7(PPID:PID=__22527:::22528)] here[___8(PPID:PID=__22527:::22528)] here[___9(PPID:PID=__22527:::22528)] here[__10(PPID:PID=__22527:::22528)] here[__11(PPID:PID=__22527:::22528)] here[__12(PPID:PID=__22527:::22528)] here[__13(PPID:PID=__22527:::22528)] here[__14(PPID:PID=__22527:::22528)] here[__15(PPID:PID=__22527:::22528)] here[__16(PPID:PID=__22527:::22528)]
_____________________________Exit from ['threading']-backend
____________________________Going into ['loky']-backend
here[___0(PPID:PID=__22527:::22528)] here[___1(PPID:PID=__22527:::22528)] here[___2(PPID:PID=__22527:::22528)] here[___3(PPID:PID=__22527:::22528)] here[___4(PPID:PID=__22527:::22528)] here[___5(PPID:PID=__22527:::22528)] here[___6(PPID:PID=__22527:::22528)] here[___7(PPID:PID=__22527:::22528)] here[___8(PPID:PID=__22527:::22528)] here[___9(PPID:PID=__22527:::22528)] here[__10(PPID:PID=__22527:::22528)] here[__11(PPID:PID=__22527:::22528)] here[__12(PPID:PID=__22527:::22528)] here[__13(PPID:PID=__22527:::22528)] here[__14(PPID:PID=__22527:::22528)] here[__15(PPID:PID=__22527:::22528)] here[__16(PPID:PID=__22527:::22528)]
_____________________________Exit from ['loky']-backend
____________________________Going into ['multiprocessing']-backend
here[___0(PPID:PID=__22527:::22528)] here[___1(PPID:PID=__22527:::22528)] here[___2(PPID:PID=__22527:::22528)] here[___3(PPID:PID=__22527:::22528)] here[___4(PPID:PID=__22527:::22528)] here[___5(PPID:PID=__22527:::22528)] here[___6(PPID:PID=__22527:::22528)] here[___7(PPID:PID=__22527:::22528)] here[___8(PPID:PID=__22527:::22528)] here[___9(PPID:PID=__22527:::22528)] here[__10(PPID:PID=__22527:::22528)] here[__11(PPID:PID=__22527:::22528)] here[__12(PPID:PID=__22527:::22528)] here[__13(PPID:PID=__22527:::22528)] here[__14(PPID:PID=__22527:::22528)] here[__15(PPID:PID=__22527:::22528)] here[__16(PPID:PID=__22527:::22528)]
_____________________________Exit from ['multiprocessing']-backend
[[0. 0. 0.]
[0. 0. 0.]
...
]
Also check this and this on your O/S, resp. your actual joblib
and (hidden) pickling-SER/DES-tools versions.