在运行 Python 2.7 的 Google App Engine 中,获取 Thread.Lock 需要 50% 的响应时间

Sti*_*tin 6 python performance google-app-engine multithreading python-2.7

我在 GAE 上使用 python27 并希望减少应用程序的响应时间。

根据 cProfile,运行 1.6M 函数调用需要 40 秒(显然太长)(似乎非常高)。我现在能找到的唯一线索是“{method 'acquire' of 'thread.lock' objects}”需要 40 秒中的 20 次。(注意:在 app.yaml 中将 threadsafe 设置为 false 或 true 似乎没有太大效果。)

关于我哪里出错或下一步要调查的地方有什么想法吗?我知道获取锁所花费的大量时间可能只是症状而不是原因,但如果是这种情况,我如何找到根本原因?在 cProfile 中为我的函数列出的所有其他时间和 ncalls 似乎是合理的。

我还想知道这是否是由于 GAE 在 2011 年底在 2.7 中遇到的性能问题:

作为参考,这里是一个示例 cProfile 输出(已删除行):

Profile data:
1662549 function calls (1652247 primitive calls) in 39.545 seconds
Ordered by: cumulative time
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
[lines removed]
10816   19.245    0.002   19.245    0.002 {method 'acquire' of 'thread.lock' objects}
[lines removed]
Run Code Online (Sandbox Code Playgroud)

在此先感谢您提供的任何帮助!

小智 2

如果您的应用程序的 RPC 非常繁重(对数据存储/urlfetch/等进行长时间调用),那么您会注意到在 thread.lock.acquire() 中等待花费了大量时间。

您可能希望在应用程序中启用AppStats,并查看每个 RPC 花费的时间。