Skip to content

Commit ece97ed

Browse files
committed
v0.0.3
1 parent 5a2db6e commit ece97ed

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

syncio/Multiprocessing.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*-coding:utf8;-*-
22
from multiprocessing import Process, Manager
3-
from syncio.Task import NewTask, TaskOutput
3+
from syncio.Task import NewTask, TaskOutput, TaskReturnException
44

55

66
def gather(*args: NewTask) -> TaskOutput:
@@ -10,7 +10,10 @@ def gather(*args: NewTask) -> TaskOutput:
1010

1111
def runner(tasker: NewTask, task_id: int) -> None:
1212
task_id += 1
13-
result = tasker.func(*tasker.args, **tasker.kwargs)
13+
try:
14+
result = tasker.func(*tasker.args, **tasker.kwargs)
15+
except BaseException as e:
16+
result = TaskReturnException(e)
1417
ret[f"task_{task_id}"] = result
1518

1619
for k, f in enumerate(args):

syncio/Task.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from abc import ABC, abstractmethod
33
from typing import Callable, Tuple, List, Any, Iterator, Dict
44
from typing_extensions import Self
5+
import traceback
6+
import pprint
57

68

79
class NewTask(ABC):
@@ -17,6 +19,10 @@ def __init__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> None:
1719
def __call__(self, *args: Any, **kwargs: Any) -> Self:
1820
pass
1921

22+
@abstractmethod
23+
def __repr__(self) -> str:
24+
pass
25+
2026

2127
class create_task(NewTask):
2228
def __init__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> None:
@@ -29,6 +35,12 @@ def __call__(self, *args: Any, **kwargs: Any) -> Self:
2935
self.kwargs = kwargs
3036
return self
3137

38+
def __repr__(self) -> str:
39+
if hasattr(self.func, "__name__"):
40+
return f"<NewTask '{self.func.__name__}'>"
41+
else:
42+
return f"<NewTask '{type(self.func).__name__}'>"
43+
3244

3345
class TaskOutput(dict): # type: ignore[type-arg]
3446
def __iter__(self) -> Iterator[Any]:
@@ -45,3 +57,25 @@ def __next__(self) -> Any:
4557
value: Any = self._output_list[self._index]
4658
self._index += 1
4759
return value
60+
61+
def __str__(self) -> str:
62+
return pprint.pformat(self, indent=2)
63+
64+
65+
class TaskReturnException:
66+
def __init__(self, e: BaseException) -> None:
67+
self.exception: BaseException = e
68+
69+
def print_exception(self) -> None:
70+
traceback.print_exception(
71+
type(self.exception), self.exception, self.exception.__traceback__
72+
)
73+
74+
def __repr__(self) -> str:
75+
return f"<TaskReturnException '{self.exception.__class__.__name__}'>"
76+
77+
def __bool__(self) -> bool:
78+
return False
79+
80+
def __len__(self) -> int:
81+
return 0

syncio/Threading.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*-coding:utf8;-*-
22
from threading import Thread
3-
from syncio.Task import NewTask, TaskOutput
3+
from syncio.Task import NewTask, TaskOutput, TaskReturnException
44

55

66
def thread_gather(*args: NewTask) -> TaskOutput:
@@ -9,7 +9,10 @@ def thread_gather(*args: NewTask) -> TaskOutput:
99

1010
def runner(tasker: NewTask, task_id: int) -> None:
1111
task_id += 1
12-
result = tasker.func(*tasker.args, **tasker.kwargs)
12+
try:
13+
result = tasker.func(*tasker.args, **tasker.kwargs)
14+
except BaseException as e:
15+
result = TaskReturnException(e)
1316
ret[f"task_{task_id}"] = result
1417

1518
for k, f in enumerate(args):

syncio/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*-coding:utf8;-*-
2-
from syncio.Task import create_task
2+
from syncio.Task import create_task, TaskReturnException
33
from syncio.Multiprocessing import gather
44
from syncio.Threading import thread_gather
55

6-
__all__ = ["create_task", "gather", "thread_gather"]
6+
__all__ = ["create_task", "gather", "thread_gather", "TaskReturnException"]

0 commit comments

Comments
 (0)