python concurrent ProcessPoolExecutor not working on Raspberry Pi (idle python 3.4.2)

眉间皱痕 提交于 2019-12-25 04:49:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!