'thread._local' object has no attribute
Asked Answered
T

3

7

I was trying to change the logging format by adding a context filter. My Format is like this

FORMAT = "%(asctime)s %(VAL)s %(message)s"

This is the class I use to set the VAL in the format.

class TEST:
  def __init__(self, val):
    self.test_var=threading.local()
    self.test_var.value=val
  def filter(self,record):
    record.VAL=self.test_var.value
    return True
  def setValue(self,val)
    self.test_var.value=CMDID

It works fine in a single threaded environment, but for a certain multi-threaded environment I get the error

<Fault 1: "exceptions.AttributeError:'thread._local' object has no attribute 'value'">

Can anyone tell me what's wrong here ?? and how to rectify?

Timer answered 17/12, 2013 at 17:21 Comment(0)
T
5

Well, the exception is telling you that the thread._local object returned from threading.local() doesn't have a value attribute that you can assign val to. You can confirm that by adding a dir(self.test_var) after the self.test_var=threading.local() line. That returns this for me:

['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__',
 '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
 '__str__']

Indeed, help(threading.local()) will show you the methods and attributes that do exist - value is not among them.

If you are attempting to add a new attribute, then perhaps you want:

self.test_var.__setattr__('value',val)

This will actually create the attribute and assign the value to it.

Instance attributes are not generally created simply by assigning to them, like non-instance variables are...

Tundra answered 17/12, 2013 at 19:38 Comment(0)
D
0

The attributes of threading local are per thread. If you are creating the TEST object on one thread and running filter on another, you would get this error.

I can't quite tell what you are trying to do. If you are sure you want thread local storage, you could use hasattr to check for the attribute right before access.

Try this:

  def filter(self,record):
    if not hasattr(self.test_var, 'value'):
      self.test_var.value = 'some default var'
    record.VAL=self.test_var.value
    return True
Doordie answered 3/12, 2018 at 22:29 Comment(0)
F
0
  1. Uninstall Keras version in your system

    pip uninstall keras
    
  2. Install keras version 2.2.5

    pip install keras==2.2.5
    

It worked for me.

Fennelly answered 15/4, 2020 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.