简体   繁体   English

带有Python 2.7的Google App Engine中的线程

[英]Threads in Google App Engine with Python 2.7

I have an application that was runing with threads in Python 2.5 in GAE. 我有一个正在GAE中使用Python 2.5运行线程的应用程序。 In the last month I have updated it to Python 2.7 and the threads have stoped to work. 在上个月,我已将其更新为Python 2.7,并且线程已停止工作。 I have search on Google and here to see if there is a problem now with the threads in GAE with Python 2.7, but I have not found anything. 我已经在Google上进行了搜索,并在这里查看Python 2.7中GAE中的线程是否存在问题,但是我什么都没找到。

The code is the following: 代码如下:

def post(self):

    bloqueo = thread.allocate_lock()
    bloqueo.acquire()
    thread.start_new_thread(self.principal,(bloqueo,))

def principal (self,bloqueo):

    bloqueo.acquire()

When I run the code, it stops at thread.start_new_thread(self.principal,(bloqueo,)) and gives the following error: 当我运行的代码,它停止在thread.start_new_thread(self.principal,(bloqueo,)),并提供了以下错误:

Thread running after request. Creation traceback:
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/runtime.py", line 151, in HandleRequest
error)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 328, in HandleRequest
return WsgiRequest(environ, handler_name, url, post_data, error).Handle()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~applcation/8.374533192190096875/src/arc.py", line 1938, in post
thread.start_new_thread(self.principal,(bloqueo,))
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/runtime.py", line 81, in StartNewThread
return base_start_new_thread(Run, ())

Do you know which one is the problem and how to solve it? 您知道哪个是问题以及如何解决吗?

Thank you 谢谢

threading was never supported in appengine under the python2.5 runtime. 在python2.5运行时下,appengine从未支持线程化。

It was only introduced with Python2.7 support. 它仅在Python2.7支持下引入。 So I don't see how this code could possibly have run in appengine with 2.5 runtime. 因此,我看不到该代码如何在2.5运行时的appengine中运行。

See docs 查看文件

https://developers.google.com/appengine/docs/python/python25/diff27 https://developers.google.com/appengine/docs/python/python25/diff27

Also I can't possibly see why you would need threads for front end requests but thats a seperate issue. 我也无法看到为什么您需要线程来处理前端请求,但这就是一个单独的问题。

If we look at the specific error you have, it says "Thread running after request. Creation traceback:" 如果我们查看您遇到的特定错误,它会显示“请求后正在运行线程。创建回溯:”

This means you are trying to continue running the thread after you have returned from the request handler. 这意味着从请求处理程序返回后,您将尝试继续运行线程。 You can not do this with appengine. 您无法使用appengine执行此操作。 You must wait for any threads to finish and then return. 您必须等待任何线程完成然后返回。 In the main performing work with a thread in a front facing request just over complicates things. 在主要的执行工作中,线程在前面的请求中刚好使事情复杂化。 If you want some concurrency in a front facing request you should use async operations. 如果要在前端请求中进行一些并发,则应使用异步操作。 Certainly what you are trying to do will never work and won't improve performance in any way. 当然,您要尝试执行的操作永远不会起作用,也不会以任何方式提高性能。

In addition trying to use locks won't help at all if another instance is handling the same request at the same time. 此外,如果另一个实例同时处理相同的请求,尝试使用锁将完全没有帮助。 That is what transactions are for. 那就是交易的目的。

Despite the small amount of information you provided so far, I really feel you should drop the threads altogether. 尽管到目前为止您提供的信息很少,但我确实觉得您应该完全放弃这些话题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM