Receiving streaming data after implementing asyncio websockets as a class?

前端 未结 2 937
粉色の甜心
粉色の甜心 2021-01-07 04:06

My question is closely related to the following question on Stackoverflow and the documentation here. I am defining a websockets-connection as a class. Next, I

2条回答
  •  一生所求
    2021-01-07 04:30

    the problem is in the function

    first loop.run_until_complete run until the future is complete doc run_until_complete
    that mean your function receive will run only one response. run_until_complete is not a callback function!.

    so in your case the main:
    deribit.get_ticks() -> run the future instance __async__get_ticks
    so __async__get_ticks is task: let's see what the task do:
    1.open ws connection:
    2.send request
    3.wait the response of the ws
    4. print(response)
    here the task is done that why you see only one line

       async def __async__get_ticks(self):
          async with self.ws as echo:
             await echo.send(json.dumps(self.request))
             response = await echo.receive()
             print(response)
    

    after explanation: the solution will be simple: need to wrap the line response with while

    async def __async__get_ticks(self):
          async with self.ws as echo:
             await echo.send(json.dumps(self.request))
             while True:
                    response = await echo.receive()
                    print(response)
    
    

    output

    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654476817,"price":7540.54,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654477824,"price":7540.52,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654478831,"price":7540.15,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654479838,"price":7539.83,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654480845,"price":7539.2,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654481852,"price":7538.96,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654482859,"price":7538.9,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654483866,"price":7538.89,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654484873,"price":7538.47,"index_name":"btc_usd"}}}
    {"jsonrpc":"2.0","method":"subscription","params":{"channel":"deribit_price_index.btc_usd","data":{"timestamp":1587654485880,"price":7537.15,"index_name":"btc_usd"}}}
    

提交回复
热议问题