【发布时间】:2018-10-12 16:48:48
【问题描述】:
我和我的朋友已经尝试解决这个代码大约一个星期了,但没有成功。我们希望有经验的程序员提供一些反馈。
我们开发了以下代码来连接到网络套接字。我们的python脚本顺利运行了7个小时,但是7个小时后就崩溃了。我们多次收到“许多打开的文件出错”。我已经搜索了一段时间的 stackoverflow 以发现编码中的 smilar 错误,但我们无法与我们的实际问题联系起来。
我们还密切关注 proc/"pid of our python script"/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()
【问题讨论】:
-
我认为您的问题可能与使用记录器有关。 stackoverflow.com/questions/30099038/…,但老实说,这只是最有意义的部分。
-
感谢您的回复,让我在没有日志的情况下测试一下,我会在这里发布更新。
-
实际上我们已经查看了没有日志记录的版本,到那时它也不起作用。
标签: python