工人的齿轮功能仍未定义

时间:2015-06-10 06:03:51

标签: python gearman

我有非常简单的工作代码,可能会犯一些愚蠢的错误。

UPDATE tmp_table
SET count=Total
WHERE date=day AND hour=hour
FROM 
(
    select CONVERT(datetime, CONVERT(varchar, new_time, 101)) As day,
    datepart(hh,new_time) As hour,count(*) As Total
    from log_table 
    where new_time > GETDATE() - 180
    group by CONVERT(datetime, CONVERT(varchar, new_time, 101)),datepart(dd,new_time),
    datepart(hh,new_time)
    order by CONVERT(datetime, CONVERT(varchar, new_time, 101)), datepart(hh,new_time))
)

给出

class BingWorker(object):
    def __init__(self):
        self.gm_worker = gearman.GearmanWorker(['localhost:4730'])
        completed_job_request = self.gm_worker.register_task('bingmedia', callBing)

    def callBing(self, gearman_worker, gearman_job):
        print "In worker ", gearman_job.data
        return "Kools"

    def run(self):
        self.gm_worker.work()

if __name__ == '__main__':
    BingWorker().run()

任何提示?示例与http://gearman.org/examples/reverse/处的python示例非常相似。只是放入类结构

改进代码:

Traceback (most recent call last):
  File "worker.py", line 16, in <module>
    BingWorker().run()
  File "worker.py", line 6, in __init__
    completed_job_request = self.gm_worker.register_task('bingmedia', callBing)
NameError: global name 'callBing' is not defined

1 个答案:

答案 0 :(得分:1)

您需要将callBing更改为self.callBing,最好将注册移至run方法的第一行而不是__init__方法。

首先是因为callBing是对缺少全局的引用,而self.callBing是对类方法的引用。第二个原因是因为在 init 完成之前你可能会调用self.callBing,这可能是坏消息。