2

I'm using the Multiprocessing module within joblib. I'm coding an iterative algorithm where I need to call Parallel on every iteration. I suspect I'm suffering from a lot of overhead here, because I'm creating and destroying a pool of workers on every call.

The joblib documentation has a reference to this case. However, the solution doesn't appear to work on Python 2.7:

with Parallel(n_jobs=2) as parallel:
  accumulator = 0.
  n_iter = 0
  while accumulator < 1000:
    results = parallel(delayed(sqrt)(accumulator + i ** 2) for i in range(5))
    accumulator += sum(results)  # synchronization barrier
    n_iter += 1

Which yields the error message:

AttributeError: __exit__

Another stackoverflow post (Python Multiprocessing Lib Error (AttributeError: __exit__)) had a response that mentioned that the with statement in Python 2.7 requires a context manager. They suggested defining a context manager that wraps around the Parallel call. I tried the following:

from contextlib import contextmanager, closing

@contextmanager
def terminating(fn):
  try:
    yield fn
  finally:
    fn.terminate()

with terminating(Parallel(n_jobs=2)) as parallel:
  accumulator = 0.
  n_iter = 0
  while accumulator < 1000:
    results = parallel(delayed(sqrt)(accumulator + i ** 2) for i in range(5))
    accumulator += sum(results)  # synchronization barrier
    n_iter += 1

However, I get the following error message:

AttributeError: 'Parallel' object has no attribute 'terminate'

Does anybody know how I can properly build a context manager for Parallel in Python 2.7?

Community
  • 1
  • 1
Adam
  • 141
  • 1
  • 5

1 Answers1

4

I believe that you are seeing this error because you are using a version of joblib that does not support this syntax. It was introduced in joblib 0.9, release Oct 14 2015.

Gael Varoquaux
  • 2,466
  • 2
  • 24
  • 12
  • Thanks, that solved the problem! It didn't have any mention in the documentation about it only being in recent versions. – Adam Nov 02 '15 at 07:02