python - Websockets 服务器不会终止
问题描述
我有这个 python 程序。如果我启动它并且没有客户端连接到它,它不会终止。我尝试了control+c,control+z,control+pause,control+break,control+shift+/,向python大神祈祷,但没有。唯一会杀死它的是:
- 按 control+c,然后让客户端连接
- control+alt+delete,杀死Python.exe
...这两者都非常不方便。
只要我不必为简单的问答服务器编写 60 行代码,我完全可以为其他人放弃这些框架。
谢谢!
import websockets
import asyncio
import time
# socket settings
HOST = '127.0.0.1' # (localhost)
PORT = 62421
async def TestingStreaming(websocket, path):
while True:
await websocket.send("testing")
try:
await websocket.recv()
except:
print("Client dropped or left.")
break
time.sleep(2)
start_server = websockets.serve(TestingStreaming, host="localhost", port=62421)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
解决方案
Control+Pause 有效,但仅适用于 PowerShell。
推荐阅读
- c# - jQuery addClass to HTML Anchor 首次工作
- pine-script - Tradingview Pine 脚本为线条绘制功能提供分辨率错误
- ios - 通过 NSPersistentCloudKitContainer 更新 iCloud Scheme 到最新的 Core Data 版本
- c# - 将填充了位和字节的 ushort[] 反序列化为自定义对象
- reactjs - React Native Drawer Navigator,如何添加图标和图像?
- c++ - “条件跳转或移动取决于未初始化的值”使用 ucontext_t
- pandas - 在 Dask 中使用 pandas cut 功能
- oauth-2.0 - Amplify/Cognito oauth 联合登录(Google),不显示弹出窗口,而是在同一页面中重定向
- firebase - React-native-Firebase:“auth/network-request-failed”
- angular - 在 Angular 中向 ckeditor 添加简单的上传适配器