Restful to serve a websocket - architecture with a single thread and asyncio?

倖福魔咒の 提交于 2021-02-11 17:01:23

问题


I have a websocket that I access from python as below.

I then want to expose this websocket (with slightly altered data) over a restful interface (it cannot be a websocket for my purposes), where a client can ping it, and should get the latest fully up to date information (each time the full dataset from the beginning).

How can I avoid that the websocket from restarting each time the client pings the restful interface?

The code looks as follows:

websocket:

class WebSocketExample:
    async def _get_data(self):
        self.data= []
        async with websockets.connect(uri) as websocket:
        while True:
                try:
                    data_raw= await asyncio.wait_for(websocket.recv(), 5)
                    self.data+= data_raw
                except asyncio.TimeoutError:
                    break

    def initiate_websocket(self):
        asyncio.set_event_loop(asyncio.new_event_loop())
        loop = asyncio.get_event_loop()
        loop.run_until_complete(self._get_data())
        loop.close()

The restful interface is using flask and simply instantiates the WebSocketExample class and gets the data back:

def restful_call():
    websocket = WebSocketExample()
    return websocket.data

The problem is that each time a new call is made via restful interface, it will need to download from the beginning again everything from the websocket.

What would be a good way to rewrite this code (conceptually) and have the websocket download everything in the background continuously into memory, and then being able to serve the restful interface at the same time? My websocket is extremely slow and the data that I get from it needs to be preprocessed. I'd like to avoid writing it into a database but only want to keep it in memory. How can I do all that while running a restful interface, which I assume would drive the process. Is there a need for multiple threads? Any suggestions of the architecture would be appreciated.

来源:https://stackoverflow.com/questions/61511708/restful-to-serve-a-websocket-architecture-with-a-single-thread-and-asyncio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!