Skip to content

Commit

Permalink
feat: patch the synchronization primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Sep 5, 2024
1 parent 4327175 commit e07a5c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/python/common/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import micropip
from js import window
from pyodide.ffi import can_run_sync, run_sync

from .package import get_package_name
from .toast import loading
Expand Down Expand Up @@ -63,6 +64,20 @@ def input(prompt=""):
builtins.input = input


@cache
def patch_sync():
if not can_run_sync():
return

import asyncio
import time

from .sync import syncify

time.sleep = wraps(time.sleep)(syncify(asyncio.sleep)) # type: ignore
asyncio.run = asyncio.get_running_loop().run_until_complete = run_sync # type: ignore


@cache
def patch_exit():
window.close.__signature__ = Signature() # type: ignore
Expand All @@ -73,4 +88,5 @@ def patch_exit():
patch_linecache()
patch_console()
patch_input()
patch_sync()
patch_exit()
10 changes: 10 additions & 0 deletions src/python/common/sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from collections.abc import Awaitable, Callable

from pyodide.ffi import run_sync


def syncify[**P, T](func: Callable[P, Awaitable[T]]) -> Callable[P, T]:
def wrapper(*args: P.args, **kwargs: P.kwargs):
return run_sync(func(*args, **kwargs))

return wrapper

0 comments on commit e07a5c7

Please sign in to comment.