Why does Google App Engine support a single thread of execution only?

Viewed 16733

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!

6 Answers

A new feature that's launched since this question was asked is background threads.

While regular threads are joined when a given request ends (they cannot outlive the requests), background threads do not have this limitation.

Background threads

Code running on manual or basic scaling instances can start a background thread that can outlive the request that spawns it. This allows instances to perform arbitrary periodic or scheduled tasks or to continue working in the background after a request has returned to the user.

Sample code:

from google.appengine.api import background_thread

# sample function to run in a background thread
def change_val(arg):
    global val
    val = arg

if auto:
    # Start the new thread in one command
    background_thread.start_new_background_thread(change_val, ['Cat'])
else:
    # create a new thread and start it
    t = background_thread.BackgroundThread(
        target=change_val, args=['Cat'])
    t.start()

Google Flexible App Engine can run any executable within a Docker container. You can Dockerize your multi-threaded executable and deploy it on Flexibile App Engine. I've done this and works as expected.

Related