Using asdict() on a dataclass with a queue.Queue() field throws TypeError

Viewed 367

When making a dictionary out of a dataclass with a queue as one of the fields, python throws a TypeError:

TypeError: cannot pickle '_thread.lock' object

to reproduce the error the following code can be used:

from dataclasses import dataclass, asdict
import queue

@dataclass
class test:
    name:str = 'SomeString'
    data:int = 12345
    Que:queue.Queue = queue.Queue()

dataClass_obj = test()
dict_obj = asdict(dataClass_obj)

note that adding a queue to a dictionary normally does not produce any error.

I need to know how to include a queue.Queue() object in a dataclass, such that using the asdict method on the dataclass wont produce the TypeError.

Since the program uses dataclasses everywhere to send parameters I am keeping dataclasses here as well instead of just using a dictionary altogether.

0 Answers
Related