我正忙于一个项目供我学习,并且不断出现此错误:

Exception in thread Thread-62:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: 'long' object is not callable


产生此错误的函数是:

teller = 0
def toRedis(dstip, srcip, dnsname):
    global teller

    ignoreDom = config.getSetting('setup', 'ignore')

    if dnsname in ignoreDom:
        pass
    else:
        teller += 1
        answer = {"_id": teller, "destination": dstip, "source": srcip, "name": dnsname}
        r_serv.hmset("_id" + str(teller), answer)

        t = threading.Thread(target=r_serv.hset("_id" + str(teller),
                    "vt", VTHandler(r_serv.hget("_id" + str(teller), "source"))))
        t.daemon = True
        t.start()

        print r_serv.hgetall("_id" + str(teller))


我很确定它来自线程,因为它在错误内部。但是我不知道出了什么问题,对我来说似乎很好。刚开始时,它并没有给我任何错误,但是大约20秒钟后,错误不断弹出,甚至在打印出这些错误的同时脚本仍在运行。

最佳答案

t = threading.Thread(target=r_serv.hset("_id" + str(teller),
                    "vt", VTHandler(r_serv.hget("_id" + str(teller), "source"))))


您正在调用r_serv.hset函数,然后将其返回值分配给target kwarg(然后将其调用并引发异常),而不是将函数本身分配给target kwarg。

您应该做的是:

t = threading.Thread(target=r_serv.hset, args=("_id" + str(teller),
                     "vt", VTHandler(r_serv.hget("_id" + str(teller), "source"))))

09-05 20:57