I have a function that I am running parallelly using joblib. Within that function there is a step that checks for an exception and writes to a file when that exception occurs. I only want to execute this step once (i.e. once on one CPU, not on all CPUs simultaneously) whenever the execption occurs and then continue with parallelly processing the rest of the code.
How can I achieve that? Basically, I want to be able to switch between using all threads and then one thread and then back again to using all threads.
from joblib import Parallel, delayed
def process(x):
try:
...some code
execpt CustomError as e:
# Need this portion of the code to be executed on one CPU only
with open("foo.txt") as f:
f.write(e)
...other code
if __name__ == '__main__':
Parallel(n_jobs=-1)(delayed(process)(i) for i in range(100))