-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removing nested-asyncio from Model Registry client
Signed-off-by: blublinsky <[email protected]>
- Loading branch information
1 parent
118d0f6
commit ec997c6
Showing
2 changed files
with
124 additions
and
30 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
clients/python/src/model_registry/_async_task_runner.py
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,102 @@ | ||
# from https://gist.github.com/blink1073/969aeba85f32c285235750626f2eadd8 | ||
|
||
""" | ||
Copyright (c) 2022 Steven Silvester | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
""" | ||
|
||
import asyncio | ||
from typing import Coroutine, Optional, Any | ||
from threading import Thread, Lock | ||
import atexit | ||
|
||
|
||
class AsyncTaskRunner: | ||
""" | ||
A singleton task runner that runs an asyncio event loop on a background thread. | ||
""" | ||
__instance = None | ||
|
||
@staticmethod | ||
def get_instance(): | ||
""" | ||
Get an AsyncTaskRunner (singleton) | ||
""" | ||
if AsyncTaskRunner.__instance is None: | ||
AsyncTaskRunner() | ||
assert AsyncTaskRunner.__instance is not None | ||
return AsyncTaskRunner.__instance | ||
|
||
def __init__(self): | ||
""" | ||
Initialize | ||
""" | ||
# make sure it is a singleton | ||
if AsyncTaskRunner.__instance is not None: | ||
raise Exception("This class is a singleton!") | ||
else: | ||
AsyncTaskRunner.__instance = self | ||
# initialize variables | ||
self.__io_loop: Optional[asyncio.AbstractEventLoop] = None | ||
self.__runner_thread: Optional[Thread] = None | ||
self.__lock = Lock() | ||
# register exit handler | ||
atexit.register(self._close) | ||
|
||
def _close(self): | ||
""" | ||
Clean up. Stop the loop if running | ||
""" | ||
if self.__io_loop: | ||
self.__io_loop.stop() | ||
|
||
def _runner(self) -> None: | ||
""" | ||
Function to run in a thread | ||
""" | ||
loop = self.__io_loop | ||
assert loop is not None | ||
try: | ||
loop.run_forever() | ||
finally: | ||
loop.close() | ||
|
||
def run(self, coro: Coroutine) -> Any: | ||
""" | ||
Synchronously run a coroutine on a background thread. | ||
""" | ||
with self.__lock: | ||
if self.__io_loop is None: | ||
# If the asyncio loop does not exist | ||
self.__io_loop = asyncio.new_event_loop() | ||
self.__runner_thread = Thread(target=self._runner, daemon=True) | ||
self.__runner_thread.start() | ||
# run coroutine thread safe inside a thread. This return concurrent future | ||
fut = asyncio.run_coroutine_threadsafe(coro, self.__io_loop) | ||
# get concurrent future result | ||
return fut.result() |
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