簡體   English   中英

將參數傳遞給python decorator

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM