-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracted event loop in a separate class
- Loading branch information
Showing
3 changed files
with
36 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import asyncio | ||
import threading | ||
from asyncio import events | ||
from threading import Thread | ||
|
||
|
||
class AblyEventLoop: | ||
loop: events | ||
thread: Thread | ||
|
||
__global_event_loop: 'AblyEventLoop' = None | ||
|
||
@staticmethod | ||
def get_global() -> 'AblyEventLoop': | ||
if AblyEventLoop.__global_event_loop is None: | ||
AblyEventLoop.__global_event_loop = AblyEventLoop() | ||
AblyEventLoop.__global_event_loop._create_if_not_exist() | ||
return AblyEventLoop.__global_event_loop | ||
|
||
def _create_if_not_exist(self): | ||
if self.loop is None: | ||
self.loop = asyncio.new_event_loop() | ||
if not self.loop.is_running(): | ||
self.thread = threading.Thread( | ||
target=self.loop.run_forever, | ||
daemon=True) | ||
self.thread.start() | ||
|
||
def close(self) -> events: | ||
self.loop.stop() | ||
self.loop.close() | ||
self.loop = None | ||
self.thread = None | ||
|