在AWS Lambda运行Inference SDK遇多进程错误,求解决方案
AWS Lambda运行Roboflow Inference SDK报错排查
问题背景
在AWS Lambda上运行调用Roboflow Inference SDK的代码时,触发了多进程相关的OSError: [Errno 38] Function not implemented错误。代码未主动使用多进程,但依赖库底层调用了多进程组件,而AWS Lambda环境不支持多进程,已耗时9小时排查未解决,需要可行的解决思路。
运行代码
import os from inference_sdk import InferenceHTTPClient def handler(event, context): client = InferenceHTTPClient(api_url="https://detect.roboflow.com", api_key=os.environ["ROBOFLOW_API_KEY"]) img_path = "./pizza.jpg" return client.infer(img_path, model_id="pizza-identifier/3")
Docker容器配置
FROM public.ecr.aws/lambda/python:3.11 RUN yum install -y mesa-libGL COPY requirements.txt ${LAMBDA_TASK_ROOT} RUN pip install -r requirements.txt COPY pizza.jpg ${LAMBDA_TASK_ROOT} COPY lambda_function.py ${LAMBDA_TASK_ROOT} CMD [ "lambda_function.handler" ]
requirements.txt内容
inference==0.9.17
完整错误信息
{ "errorMessage": "[Errno 38] Function not implemented", "errorType": "OSError", "requestId": "703be804-fd86-4b44-88f9-ac54c87717be", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 10, in handler\n return client.infer(img_path, model_id=\"pizza-identifier/3\")\n", " File \"/var/lang/lib/python3.11/site-packages/inference_sdk/http/client.py\", line 82, in decorate\n return function(*args, **kwargs)\n", " File \"/var/lang/lib/python3.11/site-packages/inference_sdk/http/client.py\", line 237, in infer\n return self.infer_from_api_v0(\n", " File \"/var/lang/lib/python3.11/site-packages/inference_sdk/http/client.py\", line 299, in infer_from_api_v0\n responses = execute_requests_packages(\n", " File \"/var/lang/lib/python3.11/site-packages/inference_sdk/http/utils/executors.py\", line 42, in execute_requests_packages\n responses = make_parallel_requests(\n", " File \"/var/lang/lib/python3.11/site-packages/inference_sdk/http/utils/executors.py\", line 58, in make_parallel_requests\n with ThreadPool(processes=workers) as pool:\n", " File \"/var/lang/lib/python3.11/multiprocessing/pool.py\", line 930, in __init__\n Pool.__init__(self, processes, initializer, initargs)\n", " File \"/var/lang/lib/python3.11/multiprocessing/pool.py\", line 196, in __init__\n self._change_notifier = self._ctx.SimpleQueue()\n", " File \"/var/lang/lib/python3.11/multiprocessing/context.py\", line 113, in SimpleQueue\n return SimpleQueue(ctx=self.get_context())\n", " File \"/var/lang/lib/python3.11/multiprocessing/queues.py\", line 341, in __init__\n self._rlock = ctx.Lock()\n", " File \"/var/lang/lib/python3.11/multiprocessing/context.py\", line 68, in Lock\n return Lock(ctx=self.get_context())\n", " File \"/var/lang/lib/python3.11/multiprocessing/synchronize.py\", line 169, in __init__\n SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)\n", " File \"/var/lang/lib/python3.11/multiprocessing/synchronize.py\", line 57, in __init__\n sl = self._semlock = _multiprocessing.SemLock(\n" ] }
解决思路
- 猴子补丁替换进程池为线程池:从栈跟踪可知,SDK误用了
multiprocessing.pool.ThreadPool(实际基于进程池实现),可以用concurrent.futures.ThreadPoolExecutor替换该类,避免触发多进程逻辑。在lambda_function.py开头添加以下代码:import multiprocessing.pool from concurrent.futures import ThreadPoolExecutor def monkey_patch_threadpool(): class ThreadPool: def __init__(self, processes, **kwargs): self.executor = ThreadPoolExecutor(max_workers=processes) def __enter__(self): return self.executor def __exit__(self, *args): self.executor.shutdown() def map(self, func, iterable): return list(self.executor.map(func, iterable)) multiprocessing.pool.ThreadPool = ThreadPool monkey_patch_threadpool() - 调整SDK版本:检查
inference库的更新日志,尝试升级到最新稳定版或降级到0.9.17之前的版本,确认是否存在底层执行器的实现修复或变更。 - 直接调用Roboflow API:绕开SDK,手动构造HTTP请求调用检测接口,完全控制请求逻辑。示例代码:
import os import requests def handler(event, context): api_key = os.environ["ROBOFLOW_API_KEY"] img_path = "./pizza.jpg" url = f"https://detect.roboflow.com/pizza-identifier/3?api_key={api_key}" with open(img_path, "rb") as f: response = requests.post(url, files={"file": f}) return response.json() - 排查运行时兼容性:尝试切换到Python 3.10的Lambda基础镜像,确认是否是Python 3.11与multiprocessing组件的兼容性问题;同时检查Lambda执行角色是否允许访问
detect.roboflow.com的网络权限。
内容的提问来源于stack exchange,提问作者Dominique Paul
相关产品推荐
相关产品推荐

