由于之前还不清楚,我发布了这个场景:
class Scraper:
def __init__(self,url):
self.start_page = url
def parse_html(self):
pass
def get_all_links(self):
pass
def run(self):
#parse html, get all links, parse them and when done...
return links
现在在像 rq 这样的任务队列中
from rq import Queue
from worker import conn
q = Queue(connection=conn)
result = q.enqueue(what_function, 'http://stackoverflow.com')
我想知道这个 what_function 是什么?我记得 Django 对他们的 CBV 做了类似的事情,所以我使用了这个类比,但不是很清楚。
我有一个像
class A:
def run(self,arg):
#do something
我需要将它传递到任务队列,所以我可以做类似的事情
a = A()
b = a.run
# q is the queue object
q.enqueue(b,some_arg)
我想知道还有什么其他方法可以做到这一点,例如,Django 在他们的基于类的视图中做到这一点,
class YourListView(ListView):
#code for your view
最终作为函数传递
your_view = YourListView.as_view()
它是如何完成的?
编辑:详细地说,django 的基于类的视图被转换为函数,因为模式函数中的参数需要一个函数。同样,您可能有一个接受以下参数的函数
task_queue(callback_function, *parameters):
#add to queue and return result when done
但是 callback_function 的功能可能主要在一个类中实现,该类有一个 run() 方法,通过该方法运行进程。