I am running a test app on Heroku with RQ. I want to fetch the result of queuing and running function in RQ and send it over to HTML. My code as follows:
Worker.py
import os
import redis
from rq import Worker, Queue, Connection
listen = ['high', 'default', 'low']
redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379')
conn = redis.from_url(redis_url)
if __name__ == '__main__':
with Connection(conn):
worker = Worker(map(Queue, listen))
worker.work()code here
App.py
q = Queue(connection=conn)
app = Flask(__name__, template_folder='template')
def somejob(y):
import time
time.sleep(3)
x = y
return x
@app.route('/pureredis', methods=['POST', 'GET'])
def pureredis():
job = q.enqueue(somejob,5000)
id = job.id
print("id",id)
job = job.fetch(id, connection=conn)
print("job",job)
status = job.get_status()
print('status',status)
if status in ['queued', 'started', 'deferred', 'failed']:
return render_template('redis_1.html', result=status)
elif status == 'finished':
result = job.result
print('result',result)
return render_template('redis_1.html',result=result)
redis_1.html
<html>
<head>
{% if refresh %}
<meta http-equiv="refresh" content="5">
{% endif %}
</head>
<body>{{ result }}</body>
</html>
When I refresh my HTML, following are my server logs:
My questions - 1) Why is the RQ status not changing and what should I do so that job gets executed. 2) What should I do to grab the result of the function (somejob()) so that I can send it to my HTML through render_template.
Simple snag in code I figured was that the status of the job was being checked too soon. Modified code: