python 浮动对象没有属性 'value'

python float object has not attribute 'value'

我有 4 个共享变量。我正在根据哪个过程更新一对 running.Following 是代码。

class App(multiprocessing.Process):
    def __init__ (self,process_id):
        multiprocessing.Process.__init__(self)
        self.process_id = process_id
   def run(self,X,Y,lock):
        while True: 
               with lock :
               #some calculations which returns x and y 
               print 'x and y returned are :',x,y
                 try:
                    X.value = x
                    Y.value = y
                 except Exception ,e:
                   print e
                 
 

if __name__ == '__main__':
    xL = Value('d',0.0)
    xR = Value('d',0.0)
    yL = Value('d',0.0)
    yR = Value('d',0.0)

    lock = Lock()

    a = A('1')
    b = B('2')
   
    process_a = Process(target = a.run, args(xL,yL,lock, ))  
    process_b = Process(target = b.run, args(xR,yR,lock, ))
   
    process_a.start()
    process_b.start()

这是输出:

x and y returned are : 375 402

'float' object has no attribute 'value'

任何帮助。 ?

看看我用括号中的数字注释的代码的三个部分:

   def run(self,X,Y,lock):
        while True:
               #some calculations which returns x and y 
               with lock :
                 X.value = x
                 Y.value = y

                 # (3) you now try to access an attribute of the arguments
                 # called 'value'
                 print X.value , Y.value , self.process_id


if __name__ == '__main__':
    xL = Value('d',0.0) # (1) these variables are assigned some objects that
    xR = Value('d',0.0) # are returned by the function 'Value'
    yL = Value('d',0.0) 
    yR = Value('d',0.0) 

    lock = Lock()

    a = A('1')
    b = B('2')

    # (2) now your variables are being passed to the 'a.run' and 'b.run'
    # methods
    process_a = Process(target = a.run, args(xL,yL,lock, ))  
    process_b = Process(target = b.run, args(xR,yR,lock, ))

    process_a.start()
    process_b.start()

当您跟踪代码的执行时,您可以看到它正在尝试访问函数 Value 返回的任何对象中名为 value 的属性。您的错误消息告诉您 Value 正在返回 float 个对象,这些对象没有 value 属性。

以下情况之一(很可能)对您来说是错误的:

  1. 函数 Value 没有按照您的想法进行操作,它在不应该的时候返回浮点数。
  2. 在您的 "some calculations which return x and y" 代码中,您用浮点数覆盖了 X 和 Y 参数。