object .__ new __(thread.lock)不安全,请使用thread.lock .__ new __() [英] object.__new__(thread.lock) is not safe, use thread.lock.__new__()

查看:91
本文介绍了object .__ new __(thread.lock)不安全,请使用thread.lock .__ new __()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到此错误,而且我不知道该如何规避.

Hi I keep getting this error and I don't know how to circumvent this.

我有以下代码:

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())

在主线程中这样启动线程

The thread is started like this in 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线程和使用线程之间的通信.这就是为什么我要存储旧版本的帐户并与新版本的帐户进行比较的原因.

There is a second thread that receives the accounts on the other side of the queue. account is a object that contains several other objects. get_balances() does an http request to get some json data. What I want to do is minimize communication between the retrieve_account_data thread and the consuming thread. That is why I'm storing the old version of account and comparing to the new version of account.

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

The other side has a similar mechanism when new account data is available, it stores the account object or if there is no new data it will use the last account data.

那么我该如何解决而又不会出错?

So how can I solve this without getting an error?

@martineau,这是eclipse不好的格式所报告的错误

@martineau here is the error reported by eclipse sorry for the bad formatting

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__()  

推荐答案

好吧,因为我只需要很少的对象数据,所以每次获得更改的Account数据后,我都会创建一个新的对象.我将其深层复制,将原件保留在线程中,然后将副本放入队列中,到目前为止似乎可以使用.

Ok since I only needed very few data of the object I create a new one every time I get changed Account data. I deepcopy this keep the original in the thread and put the copy in the queue, which seemed to work so far.

这篇关于object .__ new __(thread.lock)不安全,请使用thread.lock .__ new __()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆