Я знаю, что этот вопрос задавался несколько раз, но я не смог найти случай, похожий на мой.
У меня есть эта функция:
def load_data(list_of_files, INP_DIR, return_featues=False):
data = []
# ------- I want to multithread this block------#
for file_name in tqdm(list_of_files):
subject , features = load_subject(INP_DIR,file_name)
data.append(subject.reset_index())
# -------------#
data = pd.concat(data, axis=0, ignore_index=True)
target = data['label']
if return_featues:
return data,target, features
else:
return data,target
Вышеупомянутая функция использует load_subject
, и для вашей справки она определяется следующим образом:
def load_subject(INP_DIR,file_name):
subject= pd.read_csv(INP_DIR+ file_name, sep='|')
< do some processing ...>
return subject, features
У меня 64 ядра на процессоре, но я не могу использовать все из них.
Я пробовал это с multiprocessing
train_files= ['p011431.psv', 'p008160.psv', 'p007253.psv', 'p018373.psv', 'p017040.psv',]
from multiprocessing import Pool
if __name__ == '__main__':
with Pool(processes=64) as pool:
pool.map(load_data, train_files)
Как видите, train_files - это список имен файлов.
Когда я запускаю вышеупомянутые строки, я получаю эту ошибку:
---------------------------------------------------------------------------
RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "/anaconda3/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/anaconda3/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
TypeError: load_subject() missing 1 required positional argument: 'file_name'
"""
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-24-96a3ce89ebb8> in <module>()
2 if __name__ == '__main__':
3 with Pool(processes=2) as pool:
----> 4 pool.map(load_subject, train_files) # process data_inputs iterable with pool
/anaconda3/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)
264 in a list that is returned.
265 '''
--> 266 return self._map_async(func, iterable, mapstar, chunksize).get()
267
268 def starmap(self, func, iterable, chunksize=None):
/anaconda3/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
--> 644 raise self._value
645
646 def _set(self, i, obj):
TypeError: load_subject() missing 1 required positional argument: 'file_name'
Обновление:
После ответа Тома я смог найти другой способ передать только один аргумент.
Вот функции, и вы увидите ошибку, которую я получаю:
def load_data(list_of_files):
data = []
# ------- I want to multithread this block------#
for file_name in tqdm(list_of_files):
subject , features = load_subject(INP_DIR,file_name)
data.append(subject.reset_index())
# -------------#
data = pd.concat(data, axis=0, ignore_index=True)
target = data['label']
return data,target
def load_subject(file_name):
subject= pd.read_csv(file_name, sep='|')
< do some processing ...>
return subject, features
train_files= ['p011431.psv', 'p008160.psv', 'p007253.psv', 'p018373.psv']
from multiprocessing import Pool
if __name__ == '__main__':
with Pool(processes=64) as pool:
pool.map(load_data, train_files)
Когда я запускаю вышеуказанные строки, я получаю новую ошибку:
---------------------------------------------------------------------------
RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "/anaconda3/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/anaconda3/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "<ipython-input-21-494105028a08>", line 407, in load_data
subject , features = load_subject(file_name)
File "<ipython-input-21-494105028a08>", line 170, in load_subject
subject= pd.read_csv(file_name, sep='|')
File "/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 787, in __init__
self._make_engine(self.engine)
File "/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 1708, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 539, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 737, in pandas._libs.parsers.TextReader._get_header
File "pandas/_libs/parsers.pyx", line 932, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas/_libs/parsers.pyx", line 2112, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.
"""
The above exception was the direct cause of the following exception:
ParserError Traceback (most recent call last)
<ipython-input-22-d6dcc5840b63> in <module>()
4
5 with Pool(processes=3) as pool:
----> 6 pool.map(load_data, files)
/anaconda3/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)
264 in a list that is returned.
265 '''
--> 266 return self._map_async(func, iterable, mapstar, chunksize).get()
267
268 def starmap(self, func, iterable, chunksize=None):
/anaconda3/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
642 return self._value
643 else:
--> 644 raise self._value
645
646 def _set(self, i, obj):
ParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.
Что мне здесь не хватает? Как я могу заставить это работать должным образом?