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

标签 python multithreading

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

我有以下代码:

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/

相关文章:

c++ - 如何使线程函数自包含

multithreading - 在线程之间共享一个 mvar

javascript - 我怎样才能从这个表格内容中获取链接(我猜它是 javascript)? (没有 Selenium )

python - 如何修补在python中运行单元测试的机器上不存在的模块?

python - 如果 dict2 value = dict1 key,将整个 dict2 value 替换为 dict1 value

c# - 如何在计时器中使用异步和等待

c# - 尝试启动多个线程时索引超出数组范围

python - python函数中的反向文本

python - Django 地理信息系统 : Using location__dwithin gives "Only numeric values of degree units are allowed" however location__distance_lte works fine

java - 双重检查锁定而不创建对象