djangodjames
djangodjames

Reputation: 319

Python rq error while putting a class method on an RQ queue

I need to put a class method to on an RQ queue. But it gives an error

Here is the worker.py

import os

import redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

Here is the class in seperate file

import requests
import re

class Robots():
    def __init__(self, url):
        self.url = url


    def get_url(self):
        if self.url.endswith('/'):
            return self.url + "robots.txt" 
        else:
            return self.url + "/robots.txt"


    def get_status_code(self):
        return requests.get(self.get_url()).status_code

Here is the app.py

import time
from rq import Queue
from worker import conn
from rob import Robots
url = 'http://heroku.com'
q = Queue(connection=conn)

task = q.enqueue(Robots(url).get_status_code())
print (task.result)
time.sleep(4)
print (task.result)

When I run the code, it gives an error.

File "app.py", line 8, in <module>
    task = q.enqueue(Robots(url).get_status_code())
  File "/home/atom/Desktop/Python/Tests/herokurq/lib/python3.8/site-packages/rq/queue.py", line 381, in enqueue
    depends_on, job_id, at_front, meta, args, kwargs) = Queue.parse_args(f, *args, **kwargs)
  File "/home/atom/Desktop/Python/Tests/herokurq/lib/python3.8/site-packages/rq/queue.py", line 353, in parse_args
    if not isinstance(f, string_types) and f.__module__ == '__main__':

When I put simple function instead of class method - everything works fine. But I need to put the method. And it gives an error.

Upvotes: 1

Views: 1364

Answers (1)

djangodjames
djangodjames

Reputation: 319

I've found the problem I should use task = q.enqueue(Robots(url).get_status_code) instead of task = q.enqueue(Robots(url).get_status_code())

Upvotes: 1

Related Questions