From f8d61aa5983cc1cb4d379a7e1ae2bebaf8426504 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Wed, 3 Jan 2024 15:07:43 +0800 Subject: [PATCH] update --- test.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/test.py b/test.py index aa787e0..72433c8 100644 --- a/test.py +++ b/test.py @@ -48,28 +48,40 @@ user_sched = {} async def close(username, device): # 倒计时600秒 await asyncio.sleep(600) - user_sched.pop(username) # print(username + "结束推流") + user_sched.pop(username) close_stream(username, device) async def task_start(username, device): - # print(username + "开始推流") - push_stream(username, device) + # print(username + "创建结束推流定时任务") task = asyncio.create_task(close(username, device)) user_sched[username] = task -@app.get("/video/{username}/{device}") -async def startup(username, device): +@app.get("/start/video/{username}/{device}") +async def start(username, device): try: + # 如果定时任务不存在,那么直接进行推流。当用户退出界面时,创建任务 if username not in user_sched: - await task_start(username, device) + # print(username + "开始推流") + push_stream(username, device) + # 如果定时任务存在,那么取消上次的任务。当用户退出界面时,创建任务 else: t = user_sched[username] - # 取消重来 - t.cancel() - await task_start(username, device) + # 取消任务 + if t is not None: + # print(username + "取消定时任务") + t.cancel() + return BaseResponse(code=200, msg="success") + except Exception as e: + return BaseResponse(code=500, msg=str(e)) + + +@app.get("/stop/video/{username}/{device}") +async def stop(username, device): + try: + await task_start(username, device) return BaseResponse(code=200, msg="success") except Exception as e: return BaseResponse(code=500, msg=str(e))