简体   繁体   English

使用django-rq的异步实例方法

[英]Asynchronous instance methods using django-rq

I need a way to dispatch instance methods asynchronously using django-rq. 我需要一种使用django-rq异步调度实例方法的方法。 I tried this: 我尝试了这个:

MyClass(object):

 @job
 def my_func(self, some_arg):
   # Do some stuff

Which fails on an AttributeError because the the function is not available in the module level namespace. 由于AttributeError在模块级名称空间中不可用,该函数在AttributeError上失败。

Anyone know a good way to solve this, without writing a pass through function at the module level that instantiates the object and then calls the method? 有谁知道解决此问题的好方法,而无需在模块级别编写实例化对象然后调用方法的传递函数? That's what I've been doing but it seems so crufty. 那是我一直在做的事情,但看起来却很脆弱。

Easiest way is to make your method synchronous and call it in asynchronous function. 最简单的方法是使您的方法同步并在异步函数中调用它。

MyClass(object):
    def my_method(self, some_arg):
        # Do some stuff

@job
def my_function(instance, some_arg):
    instance.my_method(some_arg)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM