Created
April 2, 2020 06:00
-
-
Save AntiKnot/b033dc1b755af93d1684405f3ec4a567 to your computer and use it in GitHub Desktop.
python rq demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask | |
| from rq import Queue | |
| from rq.job import Job | |
| # ----- | |
| from redis import Redis | |
| from rq import Worker, Queue, Connection | |
| conn = Redis() | |
| # ----- | |
| app = Flask(__name__) | |
| q = Queue(connection=conn) | |
| @app.route('/') | |
| def index(): | |
| return "index" | |
| @app.route('/work') | |
| def work(): | |
| job = q.enqueue(foo, n=3) | |
| return job.get_id() | |
| @app.route("/results/<job_key>", methods=['GET']) | |
| def get_results(job_key): | |
| job = Job.fetch(job_key, connection=conn) | |
| if job.is_finished: | |
| return str(job.result), 200 | |
| else: | |
| return "Nay!", 202 | |
| if __name__ == '__main__': | |
| app.run(host='localhost', port='8001') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment