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开发人员,如果我们共享同一个代码,而不是强迫每个遇到这个问题的用户去开发他们自己的解决方案,那么对于世界经济来说,效率会更高,温室气体排放更少等等。我希望我已经尽了我的职责,把这个问题提炼成一个明确的问题,假设构建块已经准备好了。在
我能让它为我自己的测试工作。 我的代码基于这个链接:https://aws.amazon.com/blogs/compute/parallel-processing-in-python-with-aws-lambda/
NB1:必须增加lambda函数的内存分配。使用默认的最小数量,多处理不会提高性能。随着我的帐户可以分配的最大值(3008MB),下面的数字达到了。在
NB2:我完全忽略了并行的max进程。我的用法没有太多的元素需要处理。在
使用下面的代码,用法是:
从我的笔记本电脑上运行:
^{pr2}$从aws运行:
测试代码如下:
希望有帮助。在
相关问题 更多 >
编程相关推荐