【发布时间】:2019-05-27 16:46:46
【问题描述】:
AWS Lambda 上的 Python 不支持 multiprocessing.Pool.map(),如 this other question 中所述。请注意,另一个问题是询问为什么它不起作用。这个问题是不同的,我在问如何在缺乏底层支持的情况下模拟功能。
另一个问题的答案之一给了我们这个代码:
# Python 3.6
from multiprocessing import Pipe, Process
def myWorkFunc(data, connection):
result = None
# Do some work and store it in result
if result:
connection.send([result])
else:
connection.send([None])
def myPipedMultiProcessFunc():
# Get number of available logical cores
plimit = multiprocessing.cpu_count()
# Setup management variables
results = []
parent_conns = []
processes = []
pcount = 0
pactive = []
i = 0
for data in iterable:
# Create the pipe for parent-child process communication
parent_conn, child_conn = Pipe()
# create the process, pass data to be operated on and connection
process = Process(target=myWorkFunc, args=(data, child_conn,))
parent_conns.append(parent_conn)
process.start()
pcount += 1
if pcount == plimit: # There is not currently room for another process
# Wait until there are results in the Pipes
finishedConns = multiprocessing.connection.wait(parent_conns)
# Collect the results and remove the connection as processing
# the connection again will lead to errors
for conn in finishedConns:
results.append(conn.recv()[0])
parent_conns.remove(conn)
# Decrement pcount so we can add a new process
pcount -= 1
# Ensure all remaining active processes have their results collected
for conn in parent_conns:
results.append(conn.recv()[0])
conn.close()
# Process results as needed
能否修改此示例代码以支持multiprocessing.Pool.map()?
到目前为止我尝试了什么
我分析了上面的代码,没有看到要执行的函数的参数或数据,所以我推断它没有执行与multiprocessing.Pool.map()相同的功能。除了演示可以组装成解决方案的构建块之外,不清楚代码的作用。
这是一个“为我写代码”的问题吗?
在某种程度上是的。这个问题影响了成千上万的 Python 开发人员,如果我们所有人共享相同的代码,而不是强迫遇到这个问题的每个 SO 用户去开发,它将对世界经济更有效率,减少温室气体排放等。他们自己的解决方法。我希望我已经完成了我的职责,将其提炼成一个明确的问题,假定的构建块已准备就绪。
【问题讨论】:
标签: python amazon-web-services multiprocessing