Переопределение * импорт по всему миру для jupyter

#python #jupyter

Вопрос:

Я запускаю jupyter lab в Windows, и fastai.vision.utils.verify_images(fns) у меня возникают проблемы, потому что он звонит fastcore.parallel.parallel по умолчанию n_workers=8 . Есть много способов обойти это, но я пытался придумать блок кода, который я мог бы вставить в любую записную книжку и сделать так, чтобы все базовые вызовы parallel выполнялись n_workers=1 .

Я попробовал следующую ячейку:

 import fastcore
import sys
_fastcore = fastcore
_parallel = lambda *args, **kwargs: fastcore.parallel.parallel(*args, **kwargs, n_workers=1)
_fastcore.parallel.parallel = _parallel
sys.modules['fastcore'] = _fastcore
fastcore.parallel.parallel
 

печатание

<function __main__.<lambda>(*args, **kwargs)>

но когда я пытаюсь запустить verify_images, он все равно терпит неудачу, как будто исправления никогда не было

 ---------------------------------------------------------------------------
BrokenProcessPool                         Traceback (most recent call last)
<ipython-input-37-f1773f2c9e62> in <module>
      3 # from mock import patch
      4 # with patch('fastcore.parallel.parallel') as _parallel:
----> 5 failed = verify_images(fns)
      6 # failed = L(fns[i] for i,o in enumerate(_parallel(verify_image, fns)) if not o)
      7 failed

~anaconda3libsite-packagesfastaivisionutils.py in verify_images(fns)
     59 def verify_images(fns):
     60     "Find images in `fns` that can't be opened"
---> 61     return L(fns[i] for i,o in enumerate(parallel(verify_image, fns)) if not o)
     62 
     63 # Cell

~anaconda3libsite-packagesfastcoreparallel.py in parallel(f, items, n_workers, total, progress, pause, threadpool, timeout, chunksize, *args, **kwargs)
    121             if total is None: total = len(items)
    122             r = progress_bar(r, total=total, leave=False)
--> 123         return L(r)
    124 
    125 # Cell

~anaconda3libsite-packagesfastcorefoundation.py in __call__(cls, x, *args, **kwargs)
     95     def __call__(cls, x=None, *args, **kwargs):
     96         if not args and not kwargs and x is not None and isinstance(x,cls): return x
---> 97         return super().__call__(x, *args, **kwargs)
     98 
     99 # Cell

~anaconda3libsite-packagesfastcorefoundation.py in __init__(self, items, use_list, match, *rest)
    103     def __init__(self, items=None, *rest, use_list=False, match=None):
    104         if (use_list is not None) or not is_array(items):
--> 105             items = listify(items, *rest, use_list=use_list, match=match)
    106         super().__init__(items)
    107 

~anaconda3libsite-packagesfastcorebasics.py in listify(o, use_list, match, *rest)
     54     elif isinstance(o, list): res = o
     55     elif isinstance(o, str) or is_array(o): res = [o]
---> 56     elif is_iter(o): res = list(o)
     57     else: res = [o]
     58     if match is not None:

~anaconda3libconcurrentfuturesprocess.py in _chain_from_iterable_of_lists(iterable)
    482     careful not to keep references to yielded objects.
    483     """
--> 484     for element in iterable:
    485         element.reverse()
    486         while element:

~anaconda3libconcurrentfutures_base.py in result_iterator()
    609                     # Careful not to keep a reference to the popped future
    610                     if timeout is None:
--> 611                         yield fs.pop().result()
    612                     else:
    613                         yield fs.pop().result(end_time - time.monotonic())

~anaconda3libconcurrentfutures_base.py in result(self, timeout)
    437                 raise CancelledError()
    438             elif self._state == FINISHED:
--> 439                 return self.__get_result()
    440             else:
    441                 raise TimeoutError()

~anaconda3libconcurrentfutures_base.py in __get_result(self)
    386     def __get_result(self):
    387         if self._exception:
--> 388             raise self._exception
    389         else:
    390             return self._result

BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
 

Я подозреваю, что это связано с fastai.vision.utils использованием импорта * для fastcore. Есть ли способ достичь того, чего я хочу?

Ответ №1:

Поскольку parallel функция уже была импортирована в fastai.vision.utils модуль, правильный способ-сопоставить этот модуль, а не fastcore.parallel :

 ...  # your code for custom `parallel` function goes here

import fastai.vision.utils
fastai.vision.utils.parallel = _parallel  # assign your custom function here