thread._local object has no attribute

2024/9/28 21:24:05

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=valdef filter(self,record):record.VAL=self.test_var.valuereturn Truedef 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?

Answer

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...

https://en.xdnf.cn/q/71292.html

Related Q&A

Pytorch batch matrix vector outer product

I am trying to generate a vector-matrix outer product (tensor) using PyTorch. Assuming the vector v has size p and the matrix M has size qXr, the result of the product should be pXqXr.Example:#size: 2 …

Scraping Google Analytics by Scrapy

I have been trying to use Scrapy to get some data from Google Analytics and despite the fact that Im a complete Python newbie I have made some progress. I can now login to Google Analytics by Scrapy b…

Pandas representative sampling across multiple columns

I have a dataframe which represents a population, with each column denoting a different quality/ characteristic of that person. How can I get a sample of that dataframe/ population, which is representa…

TensorFlow - Ignore infinite values when calculating the mean of a tensor

This is probably a basic question, but I cant find a solution:I need to calculate the mean of a tensor ignoring any non-finite values.For example mean([2.0, 3.0, inf, 5.0]) should return 3.333 and not …

encode unicode characters to unicode escape sequences

Ive a CSV file containing sites along with addresses. I need to work on this file to produce a json file that I will use in Django to load initial data to my database. To do that, I need to convert all…

Python: Regarding variable scope. Why dont I need to pass x to Y?

Consider the following code, why dont I need to pass x to Y?class X: def __init__(self):self.a = 1self.b = 2self.c = 3class Y: def A(self):print(x.a,x.b,x.c)x = X() y = Y() y.A()Thank you to…

Python/Pandas - partitioning a pandas DataFrame in 10 disjoint, equally-sized subsets

I want to partition a pandas DataFrame into ten disjoint, equally-sized, randomly composed subsets. I know I can randomly sample one tenth of the original pandas DataFrame using:partition_1 = pandas.Da…

How to fix pylint error Unnecessary use of a comprehension

With python 3.8.6 and pylint 2.4.4 the following code produces a pylint error (or recommendation) R1721: Unnecessary use of a comprehension (unnecessary-comprehension)This is the code: dict1 = {"A…

conv2d_transpose is dependent on batch_size when making predictions

I have a neural network currently implemented in tensorflow, but I am having a problem making predictions after training, because I have a conv2d_transpose operations, and the shapes of these ops are d…

How SelectKBest (chi2) calculates score?

I am trying to find the most valuable features by applying feature selection methods to my dataset. Im using the SelectKBest function for now. I can generate the score values and sort them as I want, b…