我和我的朋友已经尝试解决此代码大约一个星期了,但没有成功。我们将感谢经验丰富的程序员的一些反馈。
我们开发了以下代码以连接到网络套接字。我们的python脚本可以平稳运行7个小时,但是7个小时后崩溃。我们多次遇到“许多打开文件错误”的提示。我搜索了一段时间的stackoverflow,以发现编码中的类似错误,但我们与实际问题无关。
我们还密切关注proc /“我们的python脚本的pid” / fd中是否存在打开的管道。只要达到1024,websocket连接就会中断。我们编辑了ulimit -n也增加了限制,但脚本仍然死掉。
我正在分享下面的代码,如果你们能给我们一些反馈以便我们解决长期困扰,我将不胜感激。
import time
import datetime
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from bt import Bt
from app import db
from app.models import LOG_HISTORY, BOOKING_ORDERS, BOOKING_CANCELLING,
add_row, delete_rows
import config
logger = config.Logger('bt log websocket.log')
def get_authenticateWss():
authenticated_wss = Bt(key=config.socket_api_key,
secret=config.socket_api_secret)
authenticated_wss.start()
while not authenticated_wss.conn.connected.is_set():
time.sleep(1)
authenticated_wss.authenticate()
time.sleep(5)
return authenticated_wss
def main(authenticated_wss):
while authenticated_wss.conn.connected.is_set():
booking_orders = BOOKING_ORDERS.query.all()
for booking_order in booking_orders:
payload = {
'cid': booking_order.cid,
'symbol': 't%s' % booking_order.symbol.split(":") .
[-1].strip(),
'type': "EXCHANGE LIMIT",
'amount': str(booking_order.amount),
'price': str(booking_order.price),
'hidden': 1
}
authenticated_wss.new_order(**payload)
logger.info("Creating the Order: %s" % str(payload))
db.session.delete(booking_order)
if float(booking_order.amount) >= 0:
add_row(LOG_HISTORY, [datetime.datetime.now(),
booking_order.symbol, "Buy Order", str(payload)])
else:
add_row(LOG_HISTORY, [datetime.datetime.now(),
booking_order.symbol, "Selling Order", str(payload)])
time.sleep(5)
booking_cancels = BOOKING_CANCELLING.query.all()
for booking_cancel in booking_cancels:
payload = {
'id': booking_cancel.order_id,
'cid': booking_cancel.order_cid,
'cid_date': booking_cancel.create_mts
}
authenticated_wss.cancel_order(**payload)
logger.info("Cancelling the Order: %s" % str(payload))
db.session.delete(booking_cancel)
add_row(LOG_HISTORY, [datetime.datetime.now(),
booking_cancel.symbol, "Cancelling Order", str(payload)])
time.sleep(5)
# time.sleep(10)
if __name__ == "__main__":
delete_rows(BOOKING_ORDERS)
delete_rows(BOOKING_CANCELLING)
while True:
logger.info("-------------- START ------------------")
authenticated_wss = get_authenticateWss()
try:
main(authenticated_wss)
except Exception as e:
logger.error(e)
finally:
logger.info("---------- STOP -----------------")
authenticated_wss.stop()
答案 0 :(得分:1)
我们已经解决了该问题,这完全是网络套接字兼容性问题。我们已经更新了模块的版本。