【发布时间】:2018-10-18 02:11:35
【问题描述】:
以下是使用 WebSocket 接收实时报价的代码。每次收到刻度时,都会调用回调函数 on_ticks() 并打印刻度。
我可以在 on_ticks() 函数中生成一个线程并调用 store_ticks() 函数将刻度存储在数据库中吗?如果是的话,有人可以帮忙并展示它是如何完成的吗?或者有没有其他方法可以调用 store_ticks() 函数并在每次收到刻度时存储刻度?
from kiteconnect import KiteTicker
kws = KiteTicker("your_api_key", "your_access_token")
def on_ticks(ws, ticks):
print(ticks)
def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens
ws.subscribe([738561, 5633])
def store_ticks():
# Store ticks here
def on_close(ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`
ws.stop()
# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.connect()
【问题讨论】:
标签: python multithreading websocket