GAE python线程不并行执行

Nit*_*esh 9 python google-app-engine python-2.7

我试图在GAE上使用Python创建一个简单的Web应用程序.应用程序需要为每个收到的请求生成一些线程.为此,我使用的是python的线程库.我生成所有线程,然后等待它们.

t1.start()
t2.start()
t3.start()

t1.join()
t2.join()
t3.join()
Run Code Online (Sandbox Code Playgroud)

应用程序运行正常,除了线程是串行运行而不是并发运行(通过在每个线程的run()方法的开头/结尾打印时间戳来确认).我已按照http://code.google.com/appengine/docs/python/python27/using27.html#Multithreading中的说明启用了多线程

我的app.yaml看起来像:

application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /stylesheet
  static_dir: stylesheet

- url: /javascript
  static_dir: javascript

- url: /pages
  static_dir: pages

- url: .*
  script: main.app
Run Code Online (Sandbox Code Playgroud)

我确保我的本地GoogleAppLauncher通过在首选项中明确设置路径来使用python 2.7.

我的线程的平均运行时间为2-3秒,在这里他们进行url打开调用并对结果进行一些处理.

我做错了什么,或者缺少一些配置来启用多线程?

Gui*_*sum 18

您是否在dev_appserver中或将应用程序上传到生产服务后遇到此问题?从你提到的GoogleAppLauncher看起来你可能会在dev_appserver中看到这个; dev_appserver不会模拟生产服务器的线程行为,您会惊讶地发现在部署应用程序后它可以正常工作.(如果没有,请在此处添加评论.)

另一个想法是:如果你主要等待urlfetch,你可以使用urlfetch的异步接口并行运行许多urlfetch调用:http: //code.google.com/appengine/docs/python/urlfetch/asynchronousrequests.html

这种方法不需要线程.(它仍然没有正确地并行化dev_appserver中的请求;但它确实在生产服务器上正确地执行了操作.)