gpt4 book ai didi

python - object.__new__(thread.lock) 不安全,使用 thread.lock.__new__()

转载 作者:太空狗 更新时间:2023-10-30 02:44:28 30 4
gpt4 key购买 nike

嗨,我一直收到这个错误,我不知道如何避免。

我有以下代码:

class retrieve_account_data(Thread):
_account_queue = None
_dict_of_db_logging_objects = {}

def __init__(self,name):
super(retrieve_account_data,self).__init__()
self.name = name

def set_account_queue(self,account):
self._account_queue = account

def set_db_logging_object(self,db_logging_object):
self._db_logging_object = db_logging_object

def run(self):
latest_account_object = self._account_queue.get(block = True)
latest_account_object.get_account_balances()
old_account_object = copy.deepcopy(latest_account_object)
self._account_queue.put(copy.deepcopy(old_account_object))

while True:
latest_account_object.get_account_balances()
if ((old_account_object.get_base_currency_amount() !=
latest_account_object.get_base_currency_amount()) or
(old_account_object.get_quote_currency_amount() !=
latest_account_object.get_quote_currency_amount())):

old_account_object = copy.deepcopy(latest_account_object)
self._account_queue.put(copy.deepcopy(old_account_object))

if (old_account_object.get_name_of_exchange() not in
self._dict_of_db_logging_objects):
self._dict_of_db_logging_objects[
old_account_object.get_name_of_exchange()] =
(db_logging("account_balances",
old_account_object.get_name_of_exchange()))

self._dict_of_db_logging_objects.log_account_data_to_db(
old_account_object.get_base_currency_amount(),
old_account_object.get_quote_currency_amount(),
time.time())

线程在main中是这样启动的

account_queue = Queue.Queue()
retrieve_account_data = retrieve_account_data("trade_account")
retrieve_account_data.set_account_queue(account_queue)
account_queue.put(account)
retrieve_account_data.start()

还有第二个线程接收队列另一端的帐户。 account 是一个包含多个其他对象的对象。 get_balances() 执行 http 请求以获取一些 json 数据。我想要做的是尽量减少 retrieve_account_data 线程和消费线程之间的通信。这就是我存储旧版本帐户并与新版本帐户进行比较的原因。

当新的账户数据可用时,另一方有类似的机制,它存储账户对象,或者如果没有新数据,它将使用最后的账户数据。

那么我怎样才能在不出错的情况下解决这个问题呢?

@martineau 这里是eclipse 报的错误,抱歉格式不对

Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 808, in __bootstrap_inner
self.run()
File "/Users/angus/fortunate_one/fortunate_one/src/thread_handling/process_orders.py", line 133, in run
old_account_object = copy.deepcopy(latest_account_object)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 334, in _reconstruct
state = deepcopy(state, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy
y = copier(x, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 334, in _reconstruct
state = deepcopy(state, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy
y = copier(x, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy
y = copier(x, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 298, in _deepcopy_inst
state = deepcopy(state, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy
y = copier(x, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 334, in _reconstruct
state = deepcopy(state, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy
y = copier(x, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 329, in _reconstruct
y = callable(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py", line 93, in __newobj__
return cls.__new__(cls, *args)
TypeError: object.__new__(thread.lock) is not safe, use thread.lock.__new__()

最佳答案

不要使用deepcopy(它几乎总是错误的)。 deepcopy 将复制所有引用的对象;在这种情况下,您的对象图有一些锁; deepcopy 无法复制它们,因此会出现错误。

关于python - object.__new__(thread.lock) 不安全,使用 thread.lock.__new__(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29267261/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com