Comments (17)
scheduler = APScheduler()
def your_task():
app = apscheduler.app
with app.app_context():
users = User.query.all()
This only works if the scheduler is defined within the same module as the task. For any sizeable real-life application, the job most likely life in another file, so we have to make a global variable that can be imported from the separate module that contains the job.
Related to that, why isn't the scheduler object available from within the task by default?
from flask-apscheduler.
What do you mean by within a blueprint?
A task is not related to a blueprint, you need to have access to the original Flask app instance to create a new app context.
You can access it using apscheduler.app
.
e.g:
scheduler = APScheduler()
def your_task():
app = apscheduler.app
with app.app_context():
users = User.query.all()
from flask-apscheduler.
scheduler = APScheduler() def your_task(): app = apscheduler.app with app.app_context(): users = User.query.all()
This only works if the scheduler is defined within the same module as the task. For any sizeable real-life application, the job most likely life in another file, so we have to make a global variable that can be imported from the separate module that contains the job.
Related to that, why isn't the scheduler object available from within the task by default?
This solved my issue, thanks Man :)
from flask-apscheduler.
Yes, a task is a normal function, but it is invoked within your library. We could specify that if the programmer want the scheduler object in the task function, just define a named parameter that will be assigned the scheduler object at runtime.
This will allow all task to access application context stuff without having to do a global import. Does this make sense?
from flask-apscheduler.
Hi,
I guess you are using Flask-SQLAlchemy and you want to access your model within a task.
That is a small example how to create a context within a task.
e.g:
def your_task():
with app.app_context():
users = User.query.all()
...
I will soon add an example in the examples folder.
from flask-apscheduler.
Yea! I am using flask-sqlalchemy. I tried more or less what you posted above, but haven't had any luck. I'm trying to use within a blueprint, which may also be causing issues.
from flask-apscheduler.
I just added a new example:
https://github.com/viniciuschiele/flask-apscheduler/blob/master/examples/flask_context.py
Let me know if it doesn't help you.
from flask-apscheduler.
Awesome. I think I finally just got it to work. Thanks for the help.
from flask-apscheduler.
Actually, I've been having one more issue. It seems that although everything runs great in development, in production I get an error from apscheduler that says it can't import the module.
It throws: LookupError: Error resolving reference app.utilities:backup_data_to_s3: could not import module.
Have you seen anything like this? For context, I'm using gunicorn/eventlet behind an NGINX proxy.
from flask-apscheduler.
Hard to tell, usually it happens when either the task path is incorrect or the module has something wrong.
The folder /var/www/tata/tata has a file init.py? If so, I guess your task path should be tata.app.utilities:daily_backup_data_to_s3
from flask-apscheduler.
@viniciuschiele Nope. No __init__.py
file in that directory. There is in the app
directory. The file structure is:
\app
models.py
utilities.py
__init__.py
\blueprint_1
config.py
manage.py
I think that's all of the relevant files...It could be that something in the module is wrong, though it works/executes fine locally..so I'm a bit confused.
from flask-apscheduler.
One thing you could do is to get the full path of the task using an utility from APScheduler.
from apscheduler.util import obj_to_ref
from app.utilities import daily_backup_data_to_s3
full_path = obj_to_ref(daily_backup_data_to_s3)
Run the above code in production, it will return the path of your task function.
Just for knowledge, my library uses the inverse method to get the function based on the path.
from apscheduler.util import ref_to_obj
task_func = ref_to_obj('app.utilities:daily_backup_data_to_s3')
from flask-apscheduler.
Hi @dhhagan
Any luck on it?
from flask-apscheduler.
yeah, that is what I recommend, just define your APScheduler object in a global module like init.py and in the task files you can import this object.
I think I didn't get your question, a task is just a normal function, how would it be able to access the sheduler object "by default" ?
Maybe you meant something like the current_app
from Flask?
from flask-apscheduler.
That's not simple to build because it is not my library that calls the tasks, my library just add endpoints to the Flask app to manage the tasks from APScheduler which is another library.
from flask-apscheduler.
I see. I remember looking into the function that invokes the function but now I remember that is jobs.py in the APScheduler not Flask-APScheduler. You are correct, it won't be easy at this level.
from flask-apscheduler.
scheduler = APScheduler() def your_task(): app = apscheduler.app with app.app_context(): users = User.query.all()
Isso só funciona se o planejador for definido no mesmo módulo da tarefa. Para qualquer aplicativo considerável da vida real, o trabalho provavelmente está em outro arquivo, então temos que fazer uma variável global que pode ser importada do módulo separado que contém o trabalho.
Relacionado a isso, por que o objeto planejador não está disponível na tarefa por padrão?
obrigado apos dias tentando tudo, isso resolveu meu problema.
from flask-apscheduler.
Related Issues (20)
- Flask 2.3.2 causing KeyError: 'JSONIFY_PRETTYPRINT_REGULAR' HOT 1
- Flask-APScheduler multiple jobs running at the same time when having multiple workers HOT 1
- [RFE] Package python3-flask-apscheduler into RPM
- KeyError: 'JSONIFY_PRETTYPRINT_REGULAR' when running Flask as a development server HOT 2
- Apscheduler not working with flask debug=True & --no-reload
- Flask 3.0.0 support HOT 1
- Behaviour of processpool HOT 1
- Key Error while passing redis instance as arg HOT 2
- Can't start BlockingScheduler as daemon in debug mode
- Issue with sqlalchemy (cannot join current thread and working outside of app context) HOT 4
- python3-flask-APScheduler_1.13.0.bb HOT 4
- Can I use hive database to restore jobs? HOT 2
- Coroutine as argument
- use api add task func which is not defined in the app start script failed HOT 2
- alembic migrations
- ModuleNotFoundError: No module named 'flask_apscheduler' HOT 2
- Adding a pyproject.toml to ensure compliance with updates to pip
- Query about ProcessPoolExecutor HOT 1
- `APScheduler` class initialisation HOT 1
- sudo apt install python3-flask-apscheduler WANTED HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask-apscheduler.