object .__ new __(thread.lock)不安全,请使用thread.lock .__ new __()

时间:2015-03-25 21:53:16

标签: 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())

线程在主

中像这样启动
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__()  

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

好的,因为我只需要很少的对象数据,每当我更改帐户数据时,我都会创建一个新数据。我深入研究这会将原文保留在线程中,并将副本放入队列中,这似乎是迄今为止的工作。