dsbowen / flask-worker Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
I didn't write any custom code. I just tried the tutorial and my job fails with following stacktrace:
Traceback (most recent call last):
File "/home/username/.local/lib/python3.6/site-packages/rq/worker.py", line 934, in perform_job
rv = job.perform()
File "/home/username/.local/lib/python3.6/site-packages/rq/job.py", line 686, in perform
self._result = self._execute()
File "/home/username/.local/lib/python3.6/site-packages/rq/job.py", line 692, in _execute
return self.func(*self.args, **self.kwargs)
File "/home/username/.local/lib/python3.6/site-packages/flask_worker/tasks.py", line 22, in execute
app.app_context().push()
AttributeError: 'NoneType' object has no attribute 'app_context'
Looks like pydoc locate can't find the flask app, but I don't know why. Anyways for completeness sake:
app.py which I named test2.py
from factory import create_app, db, socketio
from models import Worker, get_model
app = create_app()
# create database before first app request
@app.before_first_request
def before_first_request():
db.create_all()
@app.route('/callback')
def with_callback():
worker = get_model(Worker, 'callback')
worker.callback = 'callback_route'
if worker.job_finished:
worker.reset()
return worker()
@app.route('/callback_route')
def callback_route():
worker = get_model(Worker, 'callback')
return worker.result if worker.job_finished else worker()
if __name__ == '__main__':
socketio.run(app, host="0.0.0.0", debug=True)
models.py
from factory import db
from flask_worker import WorkerMixin
def complex_task(seconds):
import time
print('Complex task started')
for i in range(seconds):
print('Progress: {}%'.format(100.0*i/seconds))
time.sleep(1)
print('Progress: 100.0%')
print('Complex task finished')
return 'Hello, World!'
# create a Worker model with the worker mixin
class Worker(WorkerMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
def __init__(self, name):
self.name = name
# set the worker's complex task along with args and kwargs
self.set(complex_task, seconds=5)
super().__init__()
def get_model(class_, name):
query_result = class_.query.filter_by(name=name).first()
return query_result or class_(name)
factory.py
from flask_worker import Manager
from flask import Flask
from flask_socketio import SocketIO
from flask_sqlalchemy import SQLAlchemy
from redis import Redis
from rq import Queue
import eventlet
import os
db = SQLAlchemy()
eventlet.monkey_patch(socket=True)
socketio = SocketIO(asynch_mode='eventlet')
# initialize a Manager with the database and socketio
manager = Manager(db=db, socketio=socketio)
def create_app():
app = Flask(__name__, static_url_path='/static')
app.config['SQLALCHEMY_DATABASE_URI'] = (
'sqlite:///'+os.path.join(os.getcwd(), 'data.db')
)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.redis = Redis.from_url('redis://')
app.task_queue = Queue('my-task-queue', connection=app.redis)
db.init_app(app)
socketio.init_app(app, message_queue='redis://')
# initialize the manager with the application
manager.init_app(app)
return app
After changing test2.py to app.py I didn't get the same error anymore, but this seems like it's a bug, that the filename has to be app.py. But it still doesn't work, now I get following error:
Traceback (most recent call last):
File "/home/username/.local/lib/python3.6/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/home/username/.local/lib/python3.6/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/username/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/username/.local/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/username/.local/lib/python3.6/site-packages/flask/app.py", line 1953, in full_dispatch_request
return self.finalize_request(rv)
File "/home/username/.local/lib/python3.6/site-packages/flask/app.py", line 1968, in finalize_request
response = self.make_response(rv)
File "/home/username/.local/lib/python3.6/site-packages/flask/app.py", line 2098, in make_response
"The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.