diff --git a/pyproject.toml b/pyproject.toml index 0eccc28..73884a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ dependencies = [ "zstandard>=0.23.0,<0.24", "click>=8.1.8,<9", "httpx>=0.28.1,<0.29", + "outcome>=1.3.0.post0", ] [project.scripts] diff --git a/skynet/dgpu/network.py b/skynet/dgpu/network.py index e7a362c..24ac441 100755 --- a/skynet/dgpu/network.py +++ b/skynet/dgpu/network.py @@ -12,6 +12,7 @@ import trio import leap import anyio import httpx +import outcome from PIL import ( Image, @@ -36,24 +37,23 @@ from skynet.ipfs import ( REQUEST_UPDATE_TIME: int = 3 -# TODO, consider using the `outcome` lib instead? -# - it's already purpose built for exactly this, boxing (async) -# function invocations.. -# |_ https://outcome.readthedocs.io/en/latest/api.html#outcome.capture -async def failable( - fn: partial, - ret_fail=None, -): - try: - return await fn() - except ( - OSError, - json.JSONDecodeError, - anyio.BrokenResourceError, - httpx.ReadError, - leap.errors.TransactionPushError - ): - return ret_fail +async def failable(fn: partial, ret_fail=None): + o = await outcome.acapture(fn) + match o: + case outcome.Error(error=( + OSError() | + json.JSONDecodeError() | + anyio.BrokenResourceError() | + httpx.ConnectError() | + httpx.ConnectTimeout() | + httpx.ReadError() | + httpx.ReadTimeout() | + leap.errors.TransactionPushError() + )): + return ret_fail + + case _: + return o.unwrap() # TODO, again the prefix XD diff --git a/uv.lock b/uv.lock index 51d6441..932568c 100644 --- a/uv.lock +++ b/uv.lock @@ -2232,6 +2232,7 @@ dependencies = [ { name = "httpx" }, { name = "msgspec" }, { name = "numpy" }, + { name = "outcome" }, { name = "pillow" }, { name = "protobuf" }, { name = "py-leap" }, @@ -2282,6 +2283,7 @@ requires-dist = [ { name = "httpx", specifier = ">=0.28.1,<0.29" }, { name = "msgspec", specifier = ">=0.19.0,<0.20" }, { name = "numpy", specifier = "<2.1" }, + { name = "outcome", specifier = ">=1.3.0.post0" }, { name = "pillow", specifier = ">=10.0.1,<11" }, { name = "protobuf", specifier = ">=5.29.3,<6" }, { name = "py-leap", git = "https://github.com/guilledk/py-leap.git?rev=v0.1a32" },