This commit is contained in:
xyj 2024-01-03 15:07:43 +08:00
parent baa2285244
commit f8d61aa598
1 changed files with 21 additions and 9 deletions

30
test.py
View File

@ -48,28 +48,40 @@ user_sched = {}
async def close(username, device): async def close(username, device):
# 倒计时600秒 # 倒计时600秒
await asyncio.sleep(600) await asyncio.sleep(600)
user_sched.pop(username)
# print(username + "结束推流") # print(username + "结束推流")
user_sched.pop(username)
close_stream(username, device) close_stream(username, device)
async def task_start(username, device): async def task_start(username, device):
# print(username + "开始推流") # print(username + "创建结束推流定时任务")
push_stream(username, device)
task = asyncio.create_task(close(username, device)) task = asyncio.create_task(close(username, device))
user_sched[username] = task user_sched[username] = task
@app.get("/video/{username}/{device}") @app.get("/start/video/{username}/{device}")
async def startup(username, device): async def start(username, device):
try: try:
# 如果定时任务不存在,那么直接进行推流。当用户退出界面时,创建任务
if username not in user_sched: if username not in user_sched:
await task_start(username, device) # print(username + "开始推流")
push_stream(username, device)
# 如果定时任务存在,那么取消上次的任务。当用户退出界面时,创建任务
else: else:
t = user_sched[username] t = user_sched[username]
# 取消重来 # 取消任务
t.cancel() if t is not None:
await task_start(username, device) # 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") return BaseResponse(code=200, msg="success")
except Exception as e: except Exception as e:
return BaseResponse(code=500, msg=str(e)) return BaseResponse(code=500, msg=str(e))