使用Python线程的EC2性能慢?

2024-10-02 00:35:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在REST端点中使用Python线程,这样端点就可以启动一个线程,然后在线程运行时立即向客户机返回200ok(然后,客户机轮询服务器状态以跟踪线程的进度)

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

代码如下:

    import threading
    [.....]

    # USES THREADING
    # https://stackoverflow.com/a/1239108/364966
    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
    # https://stackoverflow.com/a/1239108/364966
    # 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线程吗


Tags: data客户机returnisstatusec2端点线程

热门问题