Python,Binance和线程Websocket

时间:2018-11-24 22:26:57

标签: python websocket binance

我需要网络套接字专家的帮助。我有以下代码:

socket_list=[] # For the web socket creation
for i in range (0, (len(coin_list)-1)): # coin_list contains some pairs ('ADABTC', 'ETHBTC',...)
    socket_list.append({'symbol':coin_list[i], 'bm':'', 'conn_key':''})

# Starts the websocket
def socket_execution():
    for i in range (0, len(coin_list)-1): #Coin
        socket_list[i]['bm'] = BinanceSocketManager(client)
        socket_list[i]['conn_key'] = socket_list[i]['bm'].start_symbol_ticker_socket(socket_list[i]['symbol'], process_message)
        socket_list[i]['bm'].start()

# Stops the websocket
def socket_stop():
    for i in range (0, len(coin_list)-1):
        socket_list[i]['bm'].stop_socket(socket_list[i]['conn_key'])

# websocket code
def process_message(msg):
    print(msg['s'] + '-' + str(b))
    #print('\n')

# Some function I created
    for x in range (1, 10):
        print (x)
    return 1

# main code
for b in range (1, 3): # I want to run the code three times
    wait = myfunction() # THIS IS THE PROBLEM! IT OPENS THE SOCKET WHILE IS PRINTING X
    while wait==0:
        time.sleep(0.1)
    print('Starting socket')
    socket_execution()
    time.sleep(60) # I want the socket to be opened for a minute
    socket_stop()
    print ('Socket stopped')
    time.sleep(5)

reactor.stop()

但是它一直在X之间打印消息。¿如何解决?我希望它先做一件事情,然后再做一件事情,但它会继续在单独的线程上进行。

0 个答案:

没有答案