GAE python线程不能并行执行 [英] GAE python threads not executing in parallel

查看:105
本文介绍了GAE python线程不能并行执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

t1.join()
t2.join()
t3.join()

应用程序运行良好,除了线程正在串行运行而不是并发运行(通过在每个线程的开始/结束处打印时间戳run()方法)。我遵循了 http://code.google.com/appengine/docs中的说明/python/python27/using27.html#Multithreading 启用多线程处理



我的app.yaml如下所示:

 应用程序:myapp 
版本:1
运行时:python27
api_version:1
线程安全:true

处理程序:
- url:/favicon\.ico
static_files:favicon.ico
上传:favicon\.ico
$ b - url:/ stylesheet
static_dir:stylesheet
$ b $ - url:/ javascript
static_dir:javascript

- url:/ pages
static_dir:pages

- url:。*
script:main.app

确保我的本地GoogleAppLauncher通过在偏好设置中明确设置路径来使用python 2.7。



我的线程的平均运行时间为2-3秒,网址打开电话和对结果做一些处理。



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

解决方案

您是否在dev_appserver中或将您的应用程序上传到生产服务后遇到此问题?从您提到的GoogleAppLauncher开始,听起来您可能会在dev_appserver中看到这一点; dev_appserver不会模拟生产服务器的线程行为,并且在部署应用程序后,您会惊讶地发现它工作得很好。 (如果没有,请在此处添加注释。)另一个想法是:如果您主要在等待urlfetch,则可以使用异步界面并行运行多个urlfetch调用到urlfetch:
http://code.google.com/appengine/docs/python /urlfetch/asynchronousrequests.html



这种方法不需要线程。 (它仍然不能很好地并行处理dev_appserver中的请求;但它确实在生产服务器上正常运行。)


I am trying to create a simple web app using Python on GAE. The app needs to spawn some threads per request received. For this I am using python's threading library. I spawn all the threads and then wait on them.

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

t1.join()
t2.join()
t3.join()

The application runs fine except for the fact that the threads are running serially rather than concurrently(confirmed this by printing the timestamps at the beginning/end of each thread's run() method). I have followed the instructions given in http://code.google.com/appengine/docs/python/python27/using27.html#Multithreading to enable multithreading

My app.yaml looks like:

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

I made sure that my local GoogleAppLauncher uses python 2.7 by setting the path explicitly in the preferences.

My threads have an average run-time of 2-3 seconds in which they make a url open call and do some processing on the result.

Am I doing something wrong, or missing some configuration to enable multithreading?

解决方案

Are you experiencing this in the dev_appserver or after uploading your app to the production service? From your mention of GoogleAppLauncher it sounds like you may be seeing this in the dev_appserver; the dev_appserver does not emulate the threading behavior of the production servers, and you'd be surprised to find that it works just fine after you deploy your app. (If not, add a comment here.)

Another idea: if you are mostly waiting for the urlfetch, you can run many urlfetch calls in parallel by using the async interface to urlfetch: http://code.google.com/appengine/docs/python/urlfetch/asynchronousrequests.html

This approach does not require threads. (It still doesn't properly parallelize the requests in the dev_appserver; but it does do things properly on the production servers.)

这篇关于GAE python线程不能并行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆