使用 Python 线程降低 EC2 性能?

Slow EC2 Performance with Python Threading?

我在 REST 端点中使用 Python 线程,以便端点可以启动线程,然后在线程运行时立即 return 向客户端发送 200 OK。 (客户端然后轮询服务器状态以跟踪线程的进度)。

代码在我的本地开发系统上运行 7 秒,但在 AWS EC2 m5.large.

上需要 6 分钟

代码如下所示:

    import threading
    [.....]

    # USES THREADING
    # 
    thr = threading.Thread(target=score, args=(myArgs1, myArgs2), kwargs={})
    thr.start() # Will run "foo"
    thr.is_alive() # Will return whether function is running currently

    data = {'now creating test scores'}
    return Response(data, status=status.HTTP_200_OK)

我关闭线程以测试这是否是速度下降的原因,如下所示:

    # USES THREADING
    # 
    # thr = threading.Thread(target=score, args=(myArgs1, myArgs2), kwargs={})
    # thr.start() # Will run "foo"
    # thr.is_alive() # Will return whether function is running currently

    # FOR DEBUGGING - SKIP THREADING TO SEE IF THAT'S WHAT'S SLOWING THINGS DOWN ON EC2
    score(myArgs1, myArgs2)

    data = {'now creating test scores'}
    return Response(data, status=status.HTTP_200_OK)

...在 EC2 上 5 秒 运行。这证明我在 EC2 上处理线程的方式是速度下降的原因。

我需要在 EC2 上进行配置以更好地支持 Python 线程吗?

一位 AWS 认证顾问建议我 EC2 是 known to be slow in execution of Python threads,并建议我改用 AWS Lambda 函数。