简体   繁体   中英

Asynchronous instance methods using django-rq

I need a way to dispatch instance methods asynchronously using 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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