此代码在Windows中运行良好,但是当我将其移至vps(ubuntu16.04,python3.6)时,出现了一些问题。
import json
import time
import threading
import websocket
import requests
from datetime import datetime, timedelta, timezone
def run_in_thread(sec):
def _wrapper(func):
def __wrapper(self, *args, **kwargs):
timer = threading.Timer(sec, func, [self, *args])
timer.start()
self._ws.on_open = self.on_open
self._ws.run_forever()
return self._result
return __wrapper
return _wrapper
class CybexHistoryAPI(object):
def __init__(self, ws_endp):
self._endpoint = ws_endp
self.reset()
def reset(self):
self._ws = websocket.WebSocketApp(self._endpoint,
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close)
self._login_api_id = -1
self._history_api_id = -1
self._api_is_ready = False
self._call_id = 1
def _send_msg(self, params):
call = {"id": self._call_id, "method": "call",
"params": params}
self._ws.send(json.dumps(call))
self._call_id += 1
def on_open(self, ws):
self._send_msg([1, "login", ["",""]])
self._call_id = 1
def on_error(self, ws, error):
print('Remote node send an error [{}]'.format(error))
def on_close(self, ws):
print('Remote node closed our connection')
def on_message(self, ws, msg):
print(msg)
if self._login_api_id < 0:
result = json.loads(msg)
self._login_api_id = result['id']
self._send_msg([self._login_api_id, "history", []])
elif self._history_api_id < 0:
result = json.loads(msg)
self._history_api_id = result['result']
self._api_is_ready = True
else:
self._result = json.loads(msg)['result']
self._ws.close()
self.reset()
@run_in_thread(0.01)
def get_account_history(self, account_id, stop, limit, start):
while not self._api_is_ready:
time.sleep(0.01)
self._send_msg([self._history_api_id, "get_account_history",
[account_id, stop, limit, start]])
@run_in_thread(0.01)
def get_market_history(self, base_id, quote_id, ts, start, end):
while not self._api_is_ready:
time.sleep(0.01)
self._send_msg([self._history_api_id, "get_market_history",
[base_id, quote_id, ts, start, end]])
@run_in_thread(0.01)
def get_fill_order_history(self, base_id, quote_id, limit):
while not self._api_is_ready:
time.sleep(0.01)
self._send_msg([self._history_api_id, "get_fill_order_history",
[base_id, quote_id, limit]])
def scan_account(acc_id, max_len = 100):
api = CybexHistoryAPI('wss://shanghai.51nebula.com')
start = 0
tot_len = 0
while tot_len < max_len:
print('start at 1.11.{}'.format(start))
ret = api.get_account_history(acc_id, '1.11.1', 100, '1.11.' + str(start))
if len(ret) == 0 or int(ret[0]['id'].split('.')[-1]) == start:
break
tot_len += len(ret)
print('got {}'.format(len(ret)))
print('end at {}'.format(ret[-1]['id']))
#print(json.dumps(ret, indent=2))
start = int(ret[-1]['id'].split('.')[-1]) - 1
print(tot_len)
return
def run_test():
scan_account('1.2.38696')
if __name__ == '__main__':
run_test()
我收到以下错误:
Traceback (most recent call last):
File "ListenTransferRecord.py", line 197, in <module>
run_test()
File "ListenTransferRecord.py", line 186, in run_test
respone = scan_account(accountID,10)
File "ListenTransferRecord.py", line 147, in scan_account
ret = api.get_account_history(acc_id, '1.11.1', 100, '1.11.' + str(start))
File "ListenTransferRecord.py", line 71, in __wrapper
return self._result
AttributeError: 'CybexHistoryAPI' object has no attribute '_result'
我不明白为什么它可以在Windows中很好地工作,但是在ubuntu中却不能。
我尝试在VPS(ubuntu)中运行一些简单的代码来测试python环境是否正常。
我尝试重新安装此代码导入的所有模块。 但它仍然无法正常工作。
答案 0 :(得分:1)
您得到的错误恰好触发了已经存在 的错误-self._result
仅在特定条件下设置(在{{ 1}}方法),但是您可以在装饰器中无条件地访问它。由于某些原因(我们无法提供更多详细信息-在没有在您的实际环境中测试代码的情况下,实际上没有机会告诉您),到目前为止,您从未(在)在Windows上运行代码时偶然发现这种情况,但是错误本身并不依赖于环境本身。解决方法非常简单:只需确保始终设置else
属性 (最终设置为on_message
之类的前哨值:
_result
为什么您在不同的环境上有不同的行为,这可能是由于很多原因导致无法确定将它们全部列出...这可能是由于网络延迟以及线程之间的同步问题(特定于操作系统)引起的实施细节,月相等等,就像我说的那样,多数情况下,如果不直接访问两个环境都无法解决问题。