forked from aws/aws-lambda-python-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_multi_concurrent_utils.py
More file actions
53 lines (46 loc) · 1.48 KB
/
lambda_multi_concurrent_utils.py
File metadata and controls
53 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
"""
import os
import sys
import socket
import multiprocessing
from . import bootstrap
from .lambda_runtime_client import LambdaMultiConcurrentRuntimeClient
class MultiConcurrentRunner:
@staticmethod
def _redirect_stream_to_fd(stream_fd: int, socket_path: str):
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
s.connect(socket_path)
os.dup2(s.fileno(), stream_fd)
@classmethod
def _redirect_output(cls, socket_path: str):
for std_fd in (sys.stdout.fileno(), sys.stderr.fileno()):
cls._redirect_stream_to_fd(std_fd, socket_path)
@classmethod
def run_single(
cls, handler: str, api_addr: str, use_thread: bool, socket_path: str
):
if socket_path:
cls._redirect_output(socket_path)
client = LambdaMultiConcurrentRuntimeClient(api_addr, use_thread)
bootstrap.run(handler, client)
@classmethod
def run_concurrent(
cls,
handler: str,
api_addr: str,
use_thread: bool,
socket_path: str,
max_concurrency: int,
):
processes = []
for _ in range(max_concurrency):
p = multiprocessing.Process(
target=cls.run_single,
args=(handler, api_addr, use_thread, socket_path),
)
p.start()
processes.append(p)
for p in processes:
p.join()