user14530855
user14530855

Reputation: 115

How do I give a gevent the flask application context?

I kick off a greenlet that gets started when the app starts. It's not meant to handle any events, but it puts data into a database. Within that greenlet I spawn another greenlet to run a task after a set delay

from flask import json, current_app
import gevent

gevent.monkey.patch_all()

def calling_code():
    gevent.spawn(fn, parm1)

def fn():
    gevent.sleep(10)
    current_app.logger.debug('Handler up')

Running this gives me the runtime error that I am not working in the context.

So I tried pushing the app context before the spawn but I can't imagine that would be accessible by that spawned worker

def calling_code():
    with app.app_context():
        gevent.spawn(fn, parm1)

def fn():
    gevent.sleep(10)
    current_app.logger.debug('Event up')

Then I tried passing in the app itself

def calling_code(app):
    gevent.spawn(fn, app, parm1)

def fn(app):
    gevent.sleep(10)
    app.logger.debug('Event up')

All these produce the same runtime exception that I'm working outside the context

Upvotes: 0

Views: 88

Answers (0)

Related Questions