我目前在一个小型后端使用FastAPI。REST Api部分工作正常。下一步我需要查询一个WebSocket API并在FastAPI中处理它。我想过滤并转发传入的数据。当我启动应用程序时,websocket客户端开始运行,但实际的FastAPI在那之后就不工作了。
我怎样才能建立一个FastAPI,它可以额外的工作作为websocket客户端和服务器?
下面是我的代码:
from fastapi import FastAPI
from routers import foo
from services import bar as bar
app = FastAPI()
app.include_router(foo.router)
@app.on_event('startup')
async def startup_event():
await bar.wh_client()
class wh_client:
def __init__(self, ):
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://xx.xx.xx.xx:xxxxx/",
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
self.ws = ws
self.ws.on_open = self.on_open
self.ws.run_forever()
def on_message(self, message, stuff):
print(message)
return message
def on_error(self, error):
return error
def on_close(self):
print("### closed ###")
def run(self, *args):
global driver
driver = True
while driver:
try:
time.sleep(1)
self.ws.send('message')
except KeyboardInterrupt:
driver = False
time.sleep(1)
self.ws.close()
print("thread terminating...")
def on_open(self, foo):
thread.start_new_thread(self.run, ())
1条答案
按热度按时间jhdbpxl91#
我也遇到过同样的问题,但还没有在网上找到解决方案。我想出的解决方案看起来很愚蠢,但很有效。
RestAPI和WS通常可以共存。