Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate on_shutdown in favor of lifespan (Fixes #397) #398

Merged
merged 9 commits into from
May 20, 2024

Conversation

abhiTronix
Copy link
Owner

Brief Description

This PR proposes to deprecate the on_shutdown hooks in Starlette, in favor of the more robust and recommended lifespan approach. The lifespan approach provides a more structured and reliable way to handle startup and shutdown tasks, and it is recommended by the ASGI specification (https://asgi.readthedocs.io/en/latest/specs/lifespan.html).

Requirements / Checklist

Related Issue

#397

Context

Currently, In APIs like WebGear, Starlette uses on_shutdown hooks for running shutdown task. However, this hooks have certain limitations and are not as robust as the lifespan approach, which uses async context managers. This PR will remove on_shutdown hooks in Starlette in favor of the more robust and recommended lifespan approach.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

…o attribute 'is_capturing'' error on Windows.

- 💄 Added new `screengear_error11.png` asset.
- 🏗️ 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.
@abhiTronix abhiTronix added ENHANCEMENT ⚡ New Feature/Addition/Improvement WORK IN PROGRESS 🚧 currently been worked on. PENDING TESTS 🧪 Waiting for CI tests to complete successfully. labels May 15, 2024
@abhiTronix abhiTronix added this to the v0.3.3 milestone May 15, 2024
@abhiTronix abhiTronix self-assigned this May 15, 2024
@abhiTronix abhiTronix linked an issue May 15, 2024 that may be closed by this pull request
4 tasks
Copy link

codecov bot commented May 15, 2024

Codecov Report

Attention: Patch coverage is 74.07407% with 7 lines in your changes are missing coverage. Please review.

Project coverage is 93.15%. Comparing base (e497ac2) to head (6285688).

Files Patch % Lines
vidgear/gears/asyncio/netgear_async.py 66.66% 4 Missing ⚠️
vidgear/gears/asyncio/webgear.py 50.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           testing     #398      +/-   ##
===========================================
- Coverage    93.56%   93.15%   -0.41%     
===========================================
  Files           15       15              
  Lines         3013     3026      +13     
===========================================
  Hits          2819     2819              
- Misses         194      207      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

- 👷 Added new recommended approach of using `pytest.mark.asyncio(scope="module")` to mark all WebGear_RTC and NetGear_Async tests as asynchronous and utilize the same event loop throughout the module.
- 🗑️ Deprecated custom `event_loop` fixture overrides in WebGear_RTC and NetGear_Async tests.
- 🔥 Removed redundant `pytest.mark.asyncio` decorators from several test functions.
- ⚡️Modified `__init__` method to handle event loop more robustly:
    - Try to get the running event loop using `asyncio.get_running_loop()`
    - If no running event loop found, create a new one with `asyncio.new_event_loop()`
    - Log if creating a new event loop
- 🧑‍💻 Changed launch method to use `self.loop.create_task()` instead of `asyncio.ensure_future()`
    - Ensures the task is created using the correct event loop instance.
- ⚡️ Moved the event loop initialization code to an earlier point before setting event loop policy to ensure it is set up correctly before selecting `WindowsSelectorEventLoop` policy.
  - 💬 Background: On Windows, vidgear requires the `WindowsSelectorEventLoop`, but Python 3.8 and above defaults to the `ProactorEventLoop` which is not compatible.
- 🔥 Removed redundant python version check to set `WindowsSelectorEventLoop` policy, as minimum supported version is already `3.8`.
- 💡 Updated code comments.
- 🐛 Replaced buggy kofi widget with a button image in index.md
- 🏗️ Remove script tags from main.html and use a custom hook for adding JS
- 🔥 Remove site.webmanifest file
- 💄 Update mkdocs.yml with new configuration settings:
    - Set edit_uri for GitHub edit links
    - Add new theme features like content actions, tooltips, etc.
    - Update palette settings for light/dark mode
    - Enable new markdown extensions
    - Add custom JS hook for adding scripts on certain pages
- ♻️ Other minor tweaks and cleanups
- 👷 Add a new event_loop_policy fixture for pytest to override the event loop policy
- 🔊 Log the event loop being used for debugging
- 🔧 Mark the new fixture in relevant test functions with pytest.mark.asyncio(scope="module")
- ♻️ Remove unused imports and code cleanup

NetGear_Async:
- ⚡️ Move event loop setup and policy assignment to the beginning of `__init__` before zmq Context creation.
- 🧑‍💻 Refactored all APIs and bonus examples to use linenums and hl_lines
    - Makes it easier to highlight specific lines in code blocks
- 📝 Updated remaining code examples for picamera2 and legacy picamera backends
- 🐛 Replaced buggy kofi widget with a button image in  `help.md`
- 💄 Update announcement icon in `main.html`
- 💄 Change system mode toggle icon and name in `mkdocs.yml`
@abhiTronix abhiTronix added the DOCS 📜 Issue/PR is related to vidgear docs. label May 19, 2024
…ra=True` on unsupported system

- ⚡️ Instead of silently disabling `picamera2` API directly, PiGear now raises an error if `picamera` is unavailable or unsupported
- 🥅 Prevented incorrect initialization of `PiGear` class on unsupported 64-bit OS systems.

Docs:
- 📝 Add failure warning in various docs about `picamera` incompatibility on 64-bit OS
@abhiTronix abhiTronix merged commit 20dec0a into testing May 20, 2024
11 checks passed
@abhiTronix abhiTronix deleted the development branch May 20, 2024 17:17
@abhiTronix abhiTronix added SOLVED 🏁 This issue/PR is resolved now. Goal Achieved! and removed WORK IN PROGRESS 🚧 currently been worked on. PENDING TESTS 🧪 Waiting for CI tests to complete successfully. labels May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DOCS 📜 Issue/PR is related to vidgear docs. ENHANCEMENT ⚡ New Feature/Addition/Improvement SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Proposal]: Deprecate on_startup and on_shutdown in favor of lifespan
1 participant