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:
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:
- https://github.com/pallets/flask/blob/master/flask/ctx.py#L79
- http://flask.pocoo.org/docs/0.12/api/#flask.ctx.RequestContext.copy
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.
- Gevent loop introduces non-deterministic testing
- therefore, cannot use gevent loop to test api