-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* clean up io, introduce utils * add support for multi-label classifcation datasets. rename data generator,
- Loading branch information
Showing
6 changed files
with
106 additions
and
62 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
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
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
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
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
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,55 @@ | ||
import asyncio | ||
import functools | ||
import threading | ||
|
||
def watchdog(afunc): | ||
"""Stops all tasks if there is an error""" | ||
@functools.wraps(afunc) | ||
async def run(*args, **kwargs): | ||
try: | ||
await afunc(*args, **kwargs) | ||
except asyncio.CancelledError: | ||
return | ||
except Exception as err: | ||
print(f'exception {err}') | ||
asyncio.get_event_loop().stop() | ||
return run | ||
|
||
|
||
def get_or_create_eventloop(): | ||
try: | ||
return asyncio.get_event_loop() | ||
except RuntimeError as ex: | ||
if "There is no current event loop in thread" in str(ex): | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
return asyncio.get_event_loop() | ||
|
||
|
||
class RunThread(threading.Thread): | ||
def __init__(self, func, args, kwargs): | ||
self.func = func | ||
self.args = args | ||
self.kwargs = kwargs | ||
super().__init__() | ||
|
||
def run(self): | ||
self.result = asyncio.run(self.func(*self.args, **self.kwargs)) | ||
|
||
|
||
def run_async(func, *args, **kwargs): | ||
"""async wrapper to detect if asyncio loop is already running | ||
This is useful when already running in async thread. | ||
""" | ||
try: | ||
loop = get_or_create_eventloop() | ||
except RuntimeError: | ||
loop = None | ||
if loop and loop.is_running(): | ||
thread = RunThread(func, args, kwargs) | ||
thread.start() | ||
thread.join() | ||
return thread.result | ||
else: | ||
return asyncio.run(func(*args, **kwargs)) |