【发布时间】:2020-02-18 23:30:31
【问题描述】:
我在 REST 端点中使用 Python 线程,以便端点可以启动线程,然后在线程运行时立即向客户端返回 200 OK。 (然后客户端轮询服务器状态以跟踪线程的进度)。
代码在我的本地开发系统上运行需要 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 线程吗?
【问题讨论】:
标签: python django multithreading amazon-ec2 python-multithreading