Skip to content

Instantly share code, notes, and snippets.

@eddiecorrigall
Last active July 27, 2017 14:34
Show Gist options
  • Save eddiecorrigall/614654f138d19d0f8e4bb47157978000 to your computer and use it in GitHub Desktop.
Save eddiecorrigall/614654f138d19d0f8e4bb47157978000 to your computer and use it in GitHub Desktop.

Python Greenlets

Tutorial: http://mauveweb.co.uk/posts/2014/07/gevent-asynchronous-io-made-easy.html

Exception example: RuntimeError('application not registered on db instance and no application bound to current context',)

The app context is not transfered between threads / greenlets. Please see:

Creating request context

Another thing of note is that the request context will automatically also create an application context when it’s pushed and there is no application context for that application so far. http://flask.pocoo.org/docs/0.12/reqcontext/#how-the-context-works

The copy is only on the request context, which contains enough information to generate an application context.

Since creating such a request context is an unnecessarily expensive operation in case there is no request around, the application context was introduced. http://flask.pocoo.org/docs/0.12/appcontext/#purpose-of-the-application-context

This suggest that creating a request context is expensive, however, it is not clear if creating the same as copying.

A valid concern is whether copying the request context will result in the same db connection, which would render the greenlets useless since they will be competing for the same connection. Ideally, each greenlet will spawn with its own db connection.

Further reading, suggests that each greenlet will have its own db connection:

Note that the exception included above claims that the app context is missing. Also, that the request context will automatically create an app context if it does not exist and it is NOT copied between threads / greenlets. See quote below.

The application context is created and destroyed as necessary. It never moves between threads and it will not be shared between requests. As such it is the perfect place to store database connection information and other things. http://flask.pocoo.org/docs/0.12/appcontext/#locality-of-the-context

Therefore a new database connection automatically is created, because the app context is automatically created from the copy of the request context.

Using the Gevent in Unit Tests

  • Gevent loop introduces non-deterministic testing
  • therefore, cannot use gevent loop to test api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment