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

[Proposal]: Deprecate on_startup and on_shutdown in favor of lifespan #397

Closed
4 tasks done
abhiTronix opened this issue May 15, 2024 · 1 comment · Fixed by #398
Closed
4 tasks done

[Proposal]: Deprecate on_startup and on_shutdown in favor of lifespan #397

abhiTronix opened this issue May 15, 2024 · 1 comment · Fixed by #398
Assignees
Labels
ENHANCEMENT ⚡ New Feature/Addition/Improvement PROPOSAL 📩 A proposal/proposition SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Milestone

Comments

@abhiTronix
Copy link
Owner

Issue guidelines

Issue Checklist

  • I have searched open or closed issues and found nothing related to my idea.
  • I have read the Documentation and it doesn't mention anything about my idea.
  • To my best knowledge, my idea wouldn't break something for other users.

Describe your Idea

Currently, Starlette supports on_startup and on_shutdown hooks for running startup and shutdown tasks. However, these hooks have certain limitations and are not as robust as the lifespan approach, which uses async context managers.

Use Cases

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). This proposal aligns with the ASGI specification and promotes best practices for handling startup and shutdown tasks in Starlette applications in our APIs such as WebGear and WebGear_RTC which uses it.

Any other Relevant Information?

Related Issues:

@abhiTronix abhiTronix added ENHANCEMENT ⚡ New Feature/Addition/Improvement WORK IN PROGRESS 🚧 currently been worked on. PROPOSAL 📩 A proposal/proposition labels May 15, 2024
@abhiTronix abhiTronix added this to the v0.3.3 milestone May 15, 2024
@abhiTronix abhiTronix added this to To do in VidGear v0.3.3 via automation May 15, 2024
@abhiTronix abhiTronix self-assigned this May 15, 2024
@abhiTronix abhiTronix linked a pull request May 15, 2024 that will close this issue
6 tasks
abhiTronix added a commit that referenced this issue May 20, 2024
- 🏗️ 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`.

### NetGear_Async API:
- 🩹 Fixed event loop handling 
    - ⚡️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.
- 🚑️  Fix event loop initialization on Windows platforms 
    - ⚡️ 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.
- ⚡️ Move event loop setup and policy assignment to the beginning of `__init__` before zmq Context creation.

### PiGear API: 
- 🚑️ Modify PiGear class behavior when `enforce_legacy_picamera=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: 
- 📝 Added workaround for 'AttributeError: 'DXCamera' object has no attribute 'is_capturing'' error on Windows.
- 💄 Added new `screengear_error11.png` asset.
- 📝 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.
- 🐛 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
- 🧑‍💻 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`
- 📝 Add failure warning in various docs about `picamera` incompatibility on 64-bit OS

### CI: 
- 💚 Deprecated event_loop fixture overrides. 
    - 👷 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.
- 💚 Refactor event loop handling 
    - 👷 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
@abhiTronix
Copy link
Owner Author

Successfully fixed in commit 20dec0a

VidGear v0.3.3 automation moved this from To do to Done May 20, 2024
@abhiTronix abhiTronix added SOLVED 🏁 This issue/PR is resolved now. Goal Achieved! and removed WORK IN PROGRESS 🚧 currently been worked on. labels May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ENHANCEMENT ⚡ New Feature/Addition/Improvement PROPOSAL 📩 A proposal/proposition SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Projects
Development

Successfully merging a pull request may close this issue.

1 participant