问题
I am trying to use multi-processing in a share price machine learning application running on raspberry pi 3 (4 cores). Here is some code which illustrates the problem:
from concurrent import futures
def some_function(x):
return x + 1
def main_function(some_list):
with futures.ProcessPoolExecutor() as executor:
results = executor.map(some_function, some_list)
return results
if __name__ == '__main__':
print(main_function([1, 2, 3]))
When I run this, I immediately get a message saying the process is still running, and asking whether I want to kill it. Whether or not I press 'ok', the programme does not produce any results, or errors.
Changing the ProcessPoolExecutor to ThreadPoolExecutor solves the problem and the programme delivers the generator object as expected.
回答1:
You need to fix you logic in the following way:
from concurrent import futures
def some_function(x):
return x + 1
def main_function(some_list):
with futures.ProcessPoolExecutor() as executor:
results = executor.map(some_function, some_list)
return list(results)
if __name__ == '__main__':
print(main_function([1, 2, 3]))
The ProcessPoolExecutor.map returns a generator which needs to be consumed in order to retrieve the results. Your above logic will not iterate over it, you get your generator object printed and nothing more.
来源:https://stackoverflow.com/questions/40833928/python-concurrent-processpoolexecutor-not-working-on-raspberry-pi-idle-python-3