views:

160

answers:

4

Does anybody have an idea why Google App Engine permits only a single thread of execution for a deployed application?

I personally believe that it has something to do with the predictability of an application so that Google can size its performance more reliably. There does not appear to be any rationale posted on Google's site regarding single threaded execution hence my question.

Having an application that is already multi-threaded and presently deployed on a VM means that it is difficult for me to move to the cloud given this restriction.

EDIT: I've marked the answer below as it sounds quite plausible that threads are not permitted due to horizontal scaling requirements. Naturally threads all execute within the same process space and, as GAE can run many processes for your application, it would be difficult to share threads. That said, I still think that a small thread pool per process would be useful and might help migrate apps to the cloud. I shall request this as a feature. Thanks for the discussion!

+1  A: 

I don't know for sure, but I believe it is probably for security reasons. If they allow multiple threads, they are opening themselves up for a fork() bomb (or threading equivalent). Also, it greatly simplifies resource management - Google only needs to manage a single thread worth of resources per application.

lacqui
I think that you might be on to something with regards to resource management. However whether you allow just one thread or ten, you can still manage things via a thread pool.
Christopher Hunt
+3  A: 

I think this is a misleading question. The App Engine does not allow your application to spawn threads, but the app engine may launch multiple instances of your application or use some sort of threaded or multiprocess request handler. I don't know the specific details but without some sort of parallelism the app engine would be a pretty useless platform.

EDIT

My original answer incorrectly implied that threads are not a useful feature, they have many uses, but the majority of web developers do not manage threads within their applications. Threads are usually managed at lower levels of the application stack.

mikerobi
+1 Also given the compute-time limitations (and responsiveness demands) that applications must adhere to, the value of spawning sub-threads would be extra overhead for no benefit.
msw
Lets assume that it is already accepted that having multiple threads in an application is a good thing. For example, I have applications that receive a RESTful request, post it to a queue, have multiple consumers of that queue etc. Other types of request may come in on the queue - not just from HTTP.
Christopher Hunt
@Christopher Hunt, only a very small percentage of web developers program at such a low level, in fact it isn't even an option for PHP developers.
mikerobi
So you're stating then, that GAE doesn't support threads because they're too hard for people to understand? Sounds like an argument that Apple hard with the IoS... until they learnt that programmers actually were ok with them. I don't think that this is the reason GAE supports only one thread of execution... Just because they are there doesn't mean that you have to use them of course.
Christopher Hunt
@Christopher Hunt, I didn't say anything about threads being hard to understand, you are just making that up. If you did a random survey of web developers, the percentage who explicitly manage threads within their applications would be small. Google can't make a platform that will please everybody, and decided that they would not cater to the minority of developers who need this feature. Keep in mind, this is one of many App Engine limitations, I'm sorry the AE doesn't meet your needs, but don't assume your needs are the same as everyone else's.
mikerobi
If you can back this up, "and decided that they would not cater to the minority of developers who need this feature" then I shall award your response as being the answer.
Christopher Hunt
@Christopher The use-case you describe exactly fits a task queue, which App Engine already provides. Do you have a compelling use-case for threads in App Engine that wouldn't work better as a task queue?
Nick Johnson
@Christopher Hunt, sorry poor choice of words, I shouldn't have presented it as a proactive decision. If a restaurant only serves hamburgers, then they aren't catering to vegetarians. A decision to only sell hamburgers is not the same as decision to ignore vegetarians, but the end result is the same.
mikerobi
+5  A: 

There is a limited alternative to spawning threads in Google App Engine called task queues: http://code.google.com/appengine/docs/python/taskqueue/

EDIT

From http://code.google.com/appengine/docs/java/runtime.html#The_Sandbox:

To allow App Engine to distribute requests for applications across multiple web servers, and to prevent one application from interfering with another, the application runs in a restricted "sandbox" environment. In this environment, the application can execute code, store and query data in the App Engine datastore, use the App Engine mail, URL fetch and users services, and examine the user's web request and prepare the response.

Like other people have pointed out, threads are not supported for securities reason to sandbox applications.

There are many other restrictions within Google App Engine that forces developers to create scalable apps. I believe task queues are just another one of these restrictions because, as opposed to creating a thread on the current machine handling the HTTP request, a task is put into a queue which can then be schedule on and executed by other machines. Tasks queues allow work to shared and distributed amongst machines in a scalable manner.

AshleyS
Tasks are just a higher level of abstraction than implementing a shared queue. I'm still interested to learn why threads are not supported directly.
Christopher Hunt
OK, this is sounding quite plausible - something along the lines of horizontal scaling considerations perhaps... the more I think about it the more it makes sense. However I'm not so sure the security issue is valid... +1 for the horizontal scaling answer.
Christopher Hunt
+3  A: 

App Engine uses a request-based execution model - that is, all work is scoped to a request, be it a user-facing one or one initated by another system such as the task queue. While it would be possible to use threads in this environment, most of the use-cases where multi-threading is useful involve long-running processes, which App Engine is not designed for, or offline work, in which case you're better off using the scalable facilities App Engine provides such as the task queue.

Put another way, threads are a specific solution to a general problem. App Engine provides an alternative for most use cases in the form of the Task Queue.

Nick Johnson
Thanks for the additional perspective. I still don't think that this is the actual reason though. GAE probably limits the amount of time your request has to process anyhow, and one could readily apply constraints to the running time of threads in a thread pool. Thanks though.
Christopher Hunt
You could, but why would you use a thread pool in that circumstance? You'd have to start a new one for each request.
Nick Johnson