2

I'm trying to add a function with a decorator for scheduling its execution but I get the following error:

ValueError: This Job cannot be serialized since the reference to its callable (<function inner at 0x7f1c900527d0>) could not be determined. Consider giving a textual reference (module:function name) instead.

My function is

@my_decorator
def my_function(id=None):
   print id

and I add it as follows:

my_scheduler.add_job(function, 'interval',minutes=1)

Is it possible to add a function with decorators? Any ideas?

As a workaround I can define an inner definition and call my decorator as well but I consider it as bad solution and I would prefer using it directly

Workaround:

def outer(id=None):
   @my_decorator
   def my_function(id=None):
      print id

   my_function(id)

my_scheduler.add_job(outer, 'interval',minutes=1)
tbo
  • 9,398
  • 8
  • 40
  • 51
  • http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python Already ! – dsgdfg Aug 17 '15 at 11:44

2 Answers2

1

After some trial and error I have managed to do what I wanted with the following:

from functools import wraps

def my_decorator():
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            # do stuff
        return wrapper
    return decorator

Now the decorator is called when the trigger is triggered by apsScheduler

The thing is that with the @wraps we handle naive introspection, i.e. we update the wrapper function to look like the wrapped function

tbo
  • 9,398
  • 8
  • 40
  • 51
0

The add_job() method accepts a string reference to your callable. So:

my_scheduler.add_job('the.module:my_function', 'interval', minutes=1)
Alex Grönholm
  • 5,563
  • 29
  • 32
  • thanks Alex, the thing is that i tried that but the decorator I put in the my_function is not called – tbo Aug 18 '15 at 07:44