Skip to content

Commit

Permalink
support python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasNeugebauer committed Mar 3, 2025
1 parent e9754a5 commit 0992d57
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions learning_loop_node/helpers/run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import asyncio
from typing import Callable, ParamSpec, TypeVar
import sys
from typing import Any, Callable, TypeVar

T = TypeVar('T')
P = ParamSpec('P')

if sys.version_info >= (3, 10):
from typing import ParamSpec
P = ParamSpec('P')

async def io_bound(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
"""Run a blocking function in a thread pool executor.
This is useful for disk I/O operations that would block the event loop."""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, lambda: func(*args, **kwargs))
async def io_bound(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
"""Run a blocking function in a thread pool executor.
This is useful for disk I/O operations that would block the event loop."""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, lambda: func(*args, **kwargs))
else:
async def io_bound(func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
"""Run a blocking function in a thread pool executor.
This is useful for disk I/O operations that would block the event loop."""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, lambda: func(*args, **kwargs))

0 comments on commit 0992d57

Please sign in to comment.