19

What is the reason of such issue in joblib? 'Multiprocessing backed parallel loops cannot be nested below threads, setting n_jobs=1' What should I do to avoid such issue?

Actually I need to implement XMLRPC server which run heavy computation in background thread and report current progress through polling from UI client. It uses scikit-learn which are based on joblib.

P.S.: I've simply changed name of the thread to "MainThread" to avoid such warning and everything looks working good (run in parallel as expected without issues). What might be a problem in future for such workaround?

Alex
  • 362
  • 1
  • 3
  • 13
  • As far as I understand, the problem is that one of your threads spawns another multithreaded computation. I, personally, don't see a need for that (yes, you can use complex fork model, but why do you use parallel loops for that?) since, presumably, you're already utilizing all parallelism you have. – Artem Sobolev Dec 25 '14 at 22:26
  • I am getting a very similar error but with multiprocessing instead of threading: `Loky-backed parallel loops cannot be called in a multiprocessing, setting n_jobs=1`. Does anybody know a solution for this? – Tobias Jul 16 '21 at 08:07

2 Answers2

10

I had the same warning while doing predictions with sklearn within a thread, using a model I loaded and which was fitted with n_jobs > 1. It appears when you pickle a model it is saved with its parameters, including n_jobs.

To avoid the warning (and potential serialization cost), set n_jobs to 1 when pickling your models:

clf = joblib.load(model_filename).set_params(n_jobs=1)
1

This seems to be due to this issue in JobLib library. At the moment of writing this seems to be fixed but not released yet. As written in the question, a dirty fix would to rename the main thread back to MainThread:

threading.current_thread().name = 'MainThread'

silentser
  • 2,083
  • 2
  • 23
  • 29