import concurrent.futures
nums = [1,2,3,4,5,6,7,8,9,10]
def f(x):
return x * x
# Make sure the map and function are working
print([val for val in map(f, nums)])
# Test to make sure concurrent map is working
with concurrent.futures.ProcessPoolExecutor() as executor:
for item in executor.map(f, nums):
print(item)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Traceback (most recent call last):
File "", line 420, in run_nodebug
File "", line 13, in
File "C:\Python33\lib\concurrent\futures\_base.py", line 546, in result_iterator
yield future.result()
File "C:\Python33\lib\concurrent\futures\_base.py", line 399, in result
return self.__get_result()
File "C:\Python33\lib\concurrent\futures\_base.py", line 351, in __get_result
raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
Как заставить этот код работать должным образом? Я надеялся, что примеры сразу же сработают.
Я пытаюсь получить общее представление об этом, прежде чем создавать нужное мне приложение. Недавно я перешёл с 2.7 на 3.3.
Прямое копирование этого кода из документации Python не дало результата, как и немного более простой пример отсюда.
Это мой код, полученный из второго примера:
[code]import concurrent.futures
nums = [1,2,3,4,5,6,7,8,9,10]
def f(x): return x * x
# Make sure the map and function are working print([val for val in map(f, nums)])
# Test to make sure concurrent map is working with concurrent.futures.ProcessPoolExecutor() as executor: for item in executor.map(f, nums): print(item) [/code]
И вот результат:
[code][1, 4, 9, 16, 25, 36, 49, 64, 81, 100] Traceback (most recent call last): File "", line 420, in run_nodebug File "", line 13, in File "C:\Python33\lib\concurrent\futures\_base.py", line 546, in result_iterator yield future.result() File "C:\Python33\lib\concurrent\futures\_base.py", line 399, in result return self.__get_result() File "C:\Python33\lib\concurrent\futures\_base.py", line 351, in __get_result raise self._exception concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending. [/code]
Как заставить этот код работать должным образом? Я надеялся, что примеры сразу же сработают.