我想在Django上运行Websockets基于python的库。
例如:
# WS server example
import asyncio
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f"> {greeting}")
start_server = websockets.serve(hello, 'localhost', 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
此示例运行websockets服务器,但由于event_loop().run_forever()
部分,它阻止了Django运行。
注意:我是Django的初学者