Skip to content

Commit

Permalink
🩹 NetGear_Async: Fixed event loop handling
Browse files Browse the repository at this point in the history
- ⚡️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.
  • Loading branch information
abhiTronix committed May 16, 2024
1 parent af6d659 commit 993cdea
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions vidgear/gears/asyncio/netgear_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,12 @@ def __init__(
import_dependency_safe("uvloop", error="log")

# Retrieve event loop and assign it
self.loop = asyncio.get_event_loop()
try:
self.loop = asyncio.get_running_loop()
except RuntimeError:
# otherwise create one
logger.critical("No running event loop found. Creating a new one.")
self.loop = asyncio.new_event_loop()
# create asyncio queue if bidirectional mode activated
self.__queue = asyncio.Queue() if self.__bi_mode else None
# log eventloop for debugging
Expand All @@ -331,7 +336,7 @@ def launch(self):
"Creating NetGear_Async asynchronous server handler!"
)
# create task for Server Handler
self.task = asyncio.ensure_future(self.__server_handler())
self.task = self.loop.create_task(self.__server_handler())
# return instance
return self

Expand Down

0 comments on commit 993cdea

Please sign in to comment.