-1

これは、関数を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()

スレッドで関数を呼び出そうとしているときにこのコードを実行すると、属性エラーが発生します。これは私が得たエラーです。修正を手伝ってください。

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "code.py", line 17, in run
    self._target(*self._args)
  File "code.py", line 34, in datapaths
    (code, val) = datapaths.listDatapaths(mylib)
AttributeError: 'function' object has no attribute 'listDatapaths'
4

1 に答える 1

0

これに答えてもらうために:

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

定義した関数の属性にアクセスしようとしていますが、設定されていない可能性があります。コードを見直す必要があると思います。

于 2013-09-12T00:32:00.133 に答える