Skip to content

Commit

Permalink
Revert "use io_bound and cpu_bound functions from NiceGUI (fixes #43)"
Browse files Browse the repository at this point in the history
This reverts commit 390a461.
  • Loading branch information
falkoschindler committed Oct 24, 2023
1 parent 7f1f993 commit 7196766
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions rosys/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import subprocess
import uuid
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from concurrent.futures.process import BrokenProcessPool
from contextlib import contextmanager
from functools import wraps
from functools import partial, wraps
from pathlib import Path
from typing import Callable, Generator, Optional
from typing import Any, Callable, Generator, Optional

from nicegui import run
from psutil import Popen

from .helpers import is_stopping, is_test
Expand All @@ -23,8 +23,17 @@
log = logging.getLogger('rosys.run')


io_bound = run.io_bound
cpu_bound = run.cpu_bound
async def io_bound(callback: Callable, *args: Any, **kwargs: Any):
if is_stopping():
return
loop = asyncio.get_running_loop()
try:
return await loop.run_in_executor(thread_pool, partial(callback, *args, **kwargs))
except RuntimeError as e:
if 'cannot schedule new futures after shutdown' not in str(e):
raise
except asyncio.exceptions.CancelledError:
pass


def awaitable(func: Callable) -> Callable:
Expand All @@ -35,6 +44,22 @@ async def inner(*args, **kwargs):
return inner


async def cpu_bound(callback: Callable, *args: Any):
if is_stopping():
return
with cpu():
try:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(process_pool, callback, *args)
except BrokenProcessPool:
pass
except RuntimeError as e:
if 'cannot schedule new futures after shutdown' not in str(e):
raise
except asyncio.exceptions.CancelledError:
pass


@contextmanager
def cpu() -> Generator[None, None, None]:
id_ = str(uuid.uuid4())
Expand Down

0 comments on commit 7196766

Please sign in to comment.