[英]passing parameters to python decorator
我正在使用uwsgi装饰器(特别是cron装饰器)在特定时间做事情。 我有以下代码:
import cherrypy
import uwsgidecorators
class TestObject(object):
@cherrypy.expose
def index(self):
launchapp = self.launchapp(-1,-1,-1,-1,-1,"foobar")
return "This is a test"
@uwsgidecorators.cron(minute,hour,day,month,dayweek)
def launchapp(self,target):
print "the target is %s" %target
return
但是我得到错误:
@uwsgidecorators.cron(minute,hour,day,month,dayweek)
NameError: name 'minute' is not defined
我基本上试图在索引函数中指定cron修饰符的时序参数。 谁知道我做错了什么?
该错误与您传递参数的事实无关,它只是未定义的minute
变量。
你会得到完全相同的错误:
>>> a = 'a'
>>> print b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>>
你可能想做的是:
@uwsgidecorators.cron(minute = 5,hour = 2, # and so on.
有关更全面的示例,请查看uwsgi文档 。
- 编辑下面。
看起来你试图在命中索引后向你的crontab添加一个任务。
这不是装饰器的用例,装饰器只是说函数应该在函数定义的cron 上运行 。 基本上,当执行装饰器(并且定义了函数,未调用 )时,它会被添加到crontab中。
在您的情况下,您想要向crontab添加一个函数; 所以你应该使用像uwsgi.add_cron
这样的东西。 您可以查看装饰器代码以了解如何使用它。
但是,您不应该使用方法而是使用函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.