как создать несколько вебсокетов
Пишу приложение которое подключается к серверу.Из за ограничения сервера я могу отправить 40 подписок на одно подключение. Приходиться создавать несколько переменных websocket.Можно ли как то автоматизировать создание вебсокетов или как то упростить их создание, что бы код не повторялся, так как нужно около 15 подключений, что бы отправить 600 подписок. Мой код примерно таков:
import websocket
import json
import time
def on_open(ws):
ws.send(json.dumps({
"op": "subscribe",
"args": [
{
"instType": "SPOT",
"channel": "trade",
"instId": "ETHUSDT"
}
]
}))
def on_open2(ws):
ws.send(json.dumps({
"op": "subscribe",
"args": [
{
"instType": "SPOT",
"channel": "trade",
"instId": "BTCUSDT"
}
]
}))
def on_message2(ws, message):
try:
print(message)
except:
# если ошибка доступа к результату
print(message)
def on_message(ws, message):
try:
print(message)
except:
# если ошибка доступа к результату
print(message)
def on_close(ws, close_status_code, close_msg):
print(f'сработала функция on_close {close_status_code}')
def on_error(ws, message):
print(message)
if __name__ == "__main__":
ws = websocket.WebSocketApp("wss://ws.bitget.com/v2/ws/public",
on_message=on_message,
on_close=on_close,
on_error=on_error,
on_open=on_open
)
ws1 = websocket.WebSocketApp("wss://ws.bitget.com/v2/ws/public",
on_message=on_message2,
on_open=on_open2
)
ws.run_forever()
ws1.run_forever()
Ответы (1 шт):
Автор решения: Amgarak
→ Ссылка
Примерно так:
import websocket
import json
def on_open(ws, instId):
ws.send(json.dumps({
"op": "subscribe",
"args": [
{
"instType": "SPOT",
"channel": "trade",
"instId": instId
}
]
}))
def on_message(ws, message):
try:
print(message)
except:
print(message)
def on_close(ws, close_status_code, close_msg):
print(f'сработала функция on_close {close_status_code}')
def on_error(ws, message):
print(message)
if __name__ == "__main__":
instIds = ["ETHUSDT", "BTCUSDT"]
websocket_apps = []
for instId in instIds:
ws = websocket.WebSocketApp("wss://ws.bitget.com/v2/ws/public",
on_message=lambda ws, msg: on_message(ws, msg),
on_close=on_close,
on_error=on_error,
on_open=lambda ws: on_open(ws, instId)
)
websocket_apps.append(ws)
for ws_app in websocket_apps:
ws_app.run_forever()
Дополнение:
import websocket
import json
import threading
import time
def on_open(ws, instId):
ws.send(json.dumps({
"op": "subscribe",
"args": [
{
"instType": "SPOT",
"channel": "trade",
"instId": instId
}
]
}))
def on_message(ws, message):
try:
print(message)
except:
print(message)
def on_close(ws, close_status_code, close_msg):
print(f'сработала функция on_close {close_status_code}')
def on_error(ws, message):
print(message)
def run_socket(instId):
ws = websocket.WebSocketApp("wss://ws.bitget.com/v2/ws/public",
on_message=lambda ws, msg: on_message(ws, msg),
on_close=on_close,
on_error=on_error,
on_open=lambda ws: on_open(ws, instId)
)
ws.run_forever()
if __name__ == "__main__":
instIds = ["ETHUSDT", "BTCUSDT"]
websocket_threads = []
for instId in instIds:
thread = threading.Thread(target=run_socket, args=(instId,))
websocket_threads.append(thread)
thread.start()