AWS Lambda thread pool (lambda-thread-pool)
You cannot use "multiprocessing.Queue" or "multiprocessing.Pool" within a Python Lambda environment because the Python Lambda execution environment does not support shared memory for processes.
You will see an issue if you attempt to use Pool:
{
"errorMessage": "[Errno 38] Function not implemented",
"errorType": "OSError",
"requestId": <request_id>,
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 10, in lambda_handler\n pool = Pool(10)\n",
" File \"/var/lang/lib/python3.9/multiprocessing/pool.py\", line 927, in __init__\n Pool.__init__(self, processes, initializer, initargs)\n",
" File \"/var/lang/lib/python3.9/multiprocessing/pool.py\", line 196, in __init__\n self._change_notifier = self._ctx.SimpleQueue()\n",
" File \"/var/lang/lib/python3.9/multiprocessing/context.py\", line 113, in SimpleQueue\n return SimpleQueue(ctx=self.get_context())\n",
" File \"/var/lang/lib/python3.9/multiprocessing/queues.py\", line 341, in __init__\n self._rlock = ctx.Lock()\n",
" File \"/var/lang/lib/python3.9/multiprocessing/context.py\", line 68, in Lock\n return Lock(ctx=self.get_context())\n",
" File \"/var/lang/lib/python3.9/multiprocessing/synchronize.py\", line 162, in __init__\n SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)\n",
" File \"/var/lang/lib/python3.9/multiprocessing/synchronize.py\", line 57, in __init__\n sl = self._semlock = _multiprocessing.SemLock(\n"
]
}
AWS Lambda thread pool (lambda-thread-pool) uses "multiprocessing.Pipe" instead of "multiprocessing.Queue". It provides the ability to perform parallel execution within the AWS lambda Python execution environment.
Prerequisites
- python3
- pip
- AWS Credentials
Install
pip install lambda-thread-pool
Usage
from lambda_thread_pool import LambdaThreadPool
def test_func(index, message):
print(index, message, end='\n')
return message
def lambda_handler(event, context):
pool = LambdaThreadPool()
results = []
for i in range(10):
res = pool.apply_async(test_func, (i, f'Message: {i}'))
results.append(res)
pool.join()
for result in results:
print('Result:', result.get())
Metadata
Release files for lambda-thread-pool 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lambda-thread-pool-0.0.2.tar.gz | 2.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lambda_thread_pool-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 5.4 kB
Release files / lambda-thread-pool-0.0.2.tar.gz
| Download URL | lambda-thread-pool-0.0.2.tar.gz |
|---|---|
| Size | 2.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a562221fe7a7a33517a5f75375fa9911f0ce877277051d8074fae31ba2b81179
|
|
BLAKE2b-256 checksum How to use checksums |
04a08dc951e4403628ab3097c0be2edd8e83f0e11dcb76830554f3135dd0dbee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Release files / lambda_thread_pool-0.0.2-py3-none-any.whl
| Download URL | lambda_thread_pool-0.0.2-py3-none-any.whl |
|---|---|
| Size | 2.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
00e75842275c2391758c9ddc936aba68cdee57c0b07f1db27adf07e6591e6157
|
|
BLAKE2b-256 checksum How to use checksums |
612561265cb54dd3f6977e1dce21dab5cfa6415b717809c51a9cf75f535196db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|