2

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.

Clay Wardell
  • 14,846
  • 13
  • 44
  • 65
  • Does it need to be a method at all? Does it access any class properties? If not, consider making it a class or static method, or moving it out of the class altogether. – Daniel Roseman Mar 27 '15 at 10:18
  • It does access class properties -- this thing is a metrics event tracker, and it needs an API Key that is stored on MyClass. – Clay Wardell Mar 27 '15 at 15:45
  • 1
    Did you ever find a solution here? – Eli Sep 22 '15 at 21:09

1 Answers1

0

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)
Miketsukami
  • 524
  • 4
  • 6