drop-in replacement for multiprocessing.Pool in AWS Lambda functions (without /dev/shm shared memory)
Low maintenance activity. Last release 17 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
Consider insteadmultiprocessPythie’s Index: 6185.9M downloads/moCompare →
pip install lambda-multiprocessinglambda_multiprocessing - Multiprocessing in AWS Lambda
This library is for doing multiprocessing in AWS Lambda in python.
(This is unrelated to inline lambda functions such as f = lambda x: x*x.
That's a different kind of lambda function.)
The Problem
If you deploy Python code to an AWS Lambda function,
the multiprocessing functions in the standard library such as multiprocessing.Pool.map will not work.
For example:
from multiprocessing import Pool
def func(x):
return x*x
args = [1,2,3]
with Pool() as p:
result = p.map(func, args)
will give you:
OSError: [Errno 38] Function not implemented
This is because AWS Lambda functions are very bare bones,
and have no shared memory device (/dev/shm).
The Solution
There is a workaround using Pipes and Processes.
Amazon documented it in this blog post.
However that example is very much tied to the work being done,
it doesn't have great error handling,
and is not structured in the way you'd expect when using the normal multiprocessing library.
The purpose of this library is to take the solution from that blog post,
and turn it into a drop-in replacement for multiprocessing.Pool.
This also includes unit testing, error handling etc, to match what you get from multiprocessing.Pool.
Usage
Install with:
pip install lambda_multiprocessing
Once you've imported Pool, it acts just like multiprocessing.Pool.
Details here.
from lambda_multiprocessing import Pool
def work(x):
return x*x
with Pool() as p:
results = p.map(work, range(5))
assert results == [x*x for x in range(5)]
Note that Lambda functions usually have only 2 vCPUs. If you allocate a lot or memory you get a few more. (e.g. 3 at 5120MB, 6 at 10240MB) The performance benefit you get from multiprocessing CPU-bound tasks is equal to the number of CPUs, minus overhead. (e.g. double speed for multiprocessing with 2 vCPUs) You can get bigger performance benefits for IO-bound tasks. (e.g. uploading many files to S3, publishing many payloads to SNS etc).