属性错误:函数对象在python中没有属性

2024-06-25 22:48:01 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我调用函数10次并测量时间的代码

    import threading
    import logging, logging.handlers
    import hpclib
    import json
    import time
    from datetime import datetime
    from features import *


    class FuncThread(threading.Thread):
        def __init__(self, target, *args):
            self._target = target
            self._args = args
            threading.Thread.__init__(self)

        def run(self):
            self._target(*self._args)

    def datapaths(ipaddress, testlogfile):
        #initialize logging system
        testlogger = logging.getLogger("testlogger")
        testlogger.setLevel(logging.DEBUG)
        file = open(testlogfile,'w')
        file.close()
        # This handler writes everything to a file.
        h1 = logging.FileHandler(testlogfile)
        f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s")
        h1.setFormatter(f)
        h1.setLevel(logging.DEBUG)
        testlogger.addHandler(h1)
        mylib = hpclib.hpclib(ipaddress)
        for i in range(10):
            t1=datetime.now().time()
            (code, val) = datapaths.listDatapaths(mylib)
            t2=datetime.now().time()
            diff=t2-t1
            logger.debug('RETURN code: ', code)
            logger.debug('Time taken in seconds: ',diff.seconds)

        testlogger.removeHandler(h1)

    # Passing ipaddress of controller and log file name
    t1 = FuncThread(datapaths, "103.0.1.40", "datapaths.log")
    t1.start()
    t1.join()

当我运行这个函数时,我正在调用一个线程。 这是我得到的错误。 请帮我修理一下。在

^{pr2}$

Tags: importselftargetdatetimetimeloggingdefargs
1条回答
网友
1楼 · 发布于 2024-06-25 22:48:01

为了得到答案:

def datapaths(ipaddress, testlogfile):
        # Unecessary code reomved
        (code, val) = datapaths.listDatapaths(mylib)
        # Unecessary code reomved

您试图访问已定义但很可能未设置的函数的属性。我想你需要重新修改你的代码。在

相关问题 更多 >