Skip to content

Commit

Permalink
⚡️ Maintenance: Refactor WebGear and WebGear RTC shutdown handling
Browse files Browse the repository at this point in the history
- 🏗️ Replaced deprecated Starlette's `on_shutdown` parameter with an async context manager `lifespan` in WebGear and WebGear_RTC APIs.
- 🚚 Moved shutdown logic for VideoGear and peer RTC connections to this new `lifespan` context manager.
- 📦️ Added new `contextlib` import for using `asynccontextmanager`.

Docs:
- 📝 Update README.md with changes to JPEG compression options
  - ✏️ Replaces deprecated options (`frame_jpeg_quality`, `frame_jpeg_optimize`, `frame_jpeg_progressive`) with their newer equivalents (`jpeg_compression_quality,` `jpeg_compression_fastdct`, `jpeg_compression_fastupsample`) in WebGear usage example.
  • Loading branch information
abhiTronix committed May 15, 2024
1 parent ee11937 commit 08b583d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ from vidgear.gears.asyncio import WebGear
# various performance tweaks
options = {
"frame_size_reduction": 40,
"frame_jpeg_quality": 80,
"frame_jpeg_optimize": True,
"frame_jpeg_progressive": False,
"jpeg_compression_quality": 80,
"jpeg_compression_fastdct": True,
"jpeg_compression_fastupsample": False,
}

# initialize WebGear app
Expand Down
11 changes: 10 additions & 1 deletion vidgear/gears/asyncio/webgear.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import asyncio
import inspect
import contextlib
import numpy as np
import logging as log
from os.path import expanduser
Expand Down Expand Up @@ -421,7 +422,7 @@ def __call__(self):
routes=self.routes,
middleware=self.middleware,
exception_handlers=self.__exception_handlers,
on_shutdown=[self.shutdown],
lifespan=self.__lifespan,
)

async def __producer(self):
Expand Down Expand Up @@ -535,6 +536,14 @@ async def __server_error(self, request, exc):
)
)

@contextlib.asynccontextmanager
async def __lifespan(self, context):
try:
yield
finally:
# close Video Server
self.shutdown()

def shutdown(self):
"""
Implements a Callable to be run on application shutdown
Expand Down
24 changes: 13 additions & 11 deletions vidgear/gears/asyncio/webgear_rtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# import the necessary packages
import os
import time
import contextlib
import fractions
import asyncio
import logging as log
Expand Down Expand Up @@ -531,7 +532,7 @@ def __call__(self):
routes=self.routes,
middleware=self.middleware,
exception_handlers=self.__exception_handlers,
on_shutdown=[self.__on_shutdown],
lifespan=self.__lifespan,
)

async def __offer(self, request):
Expand Down Expand Up @@ -636,16 +637,17 @@ async def __reset_connections(self, request):
# if does, then do nothing
return PlainTextResponse("DISABLED")

async def __on_shutdown(self):
"""
Implements a Callable to be run on application shutdown
"""
# close Video Server
self.shutdown()
# collects peer RTC connections
coros = [pc.close() for pc in self.__pcs]
await asyncio.gather(*coros)
self.__pcs.clear()
@contextlib.asynccontextmanager
async def __lifespan(self, context):
try:
yield
finally:
# close Video Server
self.shutdown()
# collects peer RTC connections
coros = [pc.close() for pc in self.__pcs]
await asyncio.gather(*coros)
self.__pcs.clear()

def shutdown(self):
"""
Expand Down

0 comments on commit 08b583d

Please sign in to comment.