Skip to content

Commit

Permalink
Create to_async.py
Browse files Browse the repository at this point in the history
  • Loading branch information
xtsea committed Dec 23, 2023
1 parent 6163b82 commit 3078211
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions RyuzakiLib/to_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import asyncio
from asyncio import AbstractEventLoop
from typing import Any
from typing import Callable

class ToAsync:
def __init__(self, function: Callable, *args):
self._loop: AbstractEventLoop = asyncio.get_event_loop()
self._function: Callable = function
self._function_args: tuple = args

async def _run(self):
result: Any = await self._loop.run_in_executor(
None,
self._function,
*self._function_args,
)

return result

def __await__(self):
return self._run().__await__()

0 comments on commit 3078211

Please sign in to comment.