Flask blueprints with gevent working outside of application context

2024/7/2 15:06:43

I am trying to send emails asynchronously with in Flask with gevent via flask-mail. I am getting "working outside of application context". I am aware of with app.app_context() but I cannot get it to work with my setup.

My application is created with an application factory like this:

myproject/run_dev.py

from gevent.wsgi import WSGIServer
from my_project.app import create_app
from my_project.config import DevConfigapp = create_app(DevConfig)
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

myproject/myproject/app.py

def create_app(config=None, app_name=None, blueprints=None):app = Flask(app_name)configure_app(app, config)<other stuff>return app

And the code that I use to send emails:

myproject/myproject/mymodule/views.py

@mymodule.route('/some/path/')
def do_something():do_stuff(something)

myproject/myproject/mymodule/utils.py

def do_stuff(something):send_email(msg)@async
def send_async_email(msg):mail.send(msg)def send_mail(request_id, recipients, email_type, env=None, pool=None):msg = Message(sender=sender,recipients=recipients,subject=subject,body=body)send_async_email(msg)

myproject/myproject/decorators.py

def async(f):def wrapper(*args, **kwargs):t = Greenlet.spawn(f, *args, **kwargs)gevent.joinall([t])return wrapper

I have tried to add:

from myproject.app import create_app
app = create_app()
with app.app_context():mail.send(msg)

to send_async_email() but then I get

ImportError: cannot import name create_app

Answer

For historical purposes: putting the from myproject.app import create_app inside the send_async_email instead of at the top of with the rest of the imports solved the ImportError, because it caused a circular dependency.

https://en.xdnf.cn/q/73369.html

Related Q&A

how to optimally count elements in a python list

This is almost the same question than here, except that I am asking about the most efficient solution for a sorted result.I have a list (about 10 integers randomly between 0 and 12), for example:the_li…

How to install and run virtualenv on MacOS correctly

Hi Im a beginner of python, I dont remember when and how I installed python3.8 on my Macbook air, only knew the installed path: % which python /usr/bin/python % which python3 /usr/local/bin/python3The …

comparing a string 1-d numpy array elementwise

I have two 1-d arrays (a and b) containing strings, which I want to compare element wise to get output c like shown below. I tried converting it to set and comparing, however that does not give the cor…

Can Atom work with Python virtualenvwrapper

I want to start a Flask app. I installed virtualenvwrapper to manage the packages but I cant let Atom know that the current project should use the virtualenvs python binary.from flask import Flask, ren…

Concatenation of tuples

Normal text:Im having some problems with coding on python 3.2.1. Actually Im taking online lectures that are on python 2.5.Here is the code:x = 100 divisors = () for i in range(1,x):if x%i == 0:divisor…

How do I create a new data table in Orange?

I am using Orange (in Python) for some data mining tasks. More specifically, for clustering. Although I have gone through the tutorial and read most of the documentation, I still have a problem. All th…

How to turn off MySQL query cache while using SQLAlchemy?

I am working with a fairly large MySQL database via the SQLAlchemy library, and Id love to turn off MySQLs query caching to debug performance issues on a per-session basis. Its difficult to debug slow …

Storing an inverted index

I am working on a project on Info Retrieval. I have made a Full Inverted Index using Hadoop/Python. Hadoop outputs the index as (word,documentlist) pairs which are written on the file. For a quick acc…

How to determine whether java is installed on a system through python?

Using Python, I want to know whether Java is installed.

How should I save the model of PyTorch if I want it loadable by OpenCV dnn module

I train a simple classification model by PyTorch and load it by opencv3.3, but it throw exception and sayOpenCV Error: The function/feature is not implemented (Unsupported Lua type) in readObject, file…