mirror of
https://github.com/LiteyukiStudio/croterline.git
synced 2026-01-26 13:42:20 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bf1380b63 | |||
| 5d505d1c89 | |||
| 9c50844195 | |||
| e151a7af0c |
@@ -5,6 +5,11 @@ from magicoca.chan import Chan
|
|||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
"""
|
||||||
|
main_chan: Chan[Any] = main to sub
|
||||||
|
sub_chan: Chan[Any] = sub to main
|
||||||
|
哪个进程发送用哪个
|
||||||
|
"""
|
||||||
self.main_chan: Chan[Any] = Chan[Any]() # main to sub
|
self.main_chan: Chan[Any] = Chan[Any]() # main to sub
|
||||||
self.sub_chan: Chan[Any] = Chan[Any]() # sub to main
|
self.sub_chan: Chan[Any] = Chan[Any]() # sub to main
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
from multiprocessing import Process as _Process
|
from multiprocessing import Process as _Process
|
||||||
from typing import Callable, Any
|
from typing import Callable, Any
|
||||||
|
|
||||||
|
from mypy.nodes import TypeAlias
|
||||||
|
|
||||||
from croterline.context import Context
|
from croterline.context import Context
|
||||||
from croterline.utils import IsMainProcess
|
from croterline.utils import IsMainProcess
|
||||||
|
|
||||||
type ProcessFuncType = Callable[[tuple[Any, ...], dict[str, Any]], None]
|
ProcessFuncType: TypeAlias = Callable[[tuple[Any, ...], dict[str, Any]], None]
|
||||||
|
|
||||||
_current_ctx: "Context | None" = None # 注入当前进程上下文
|
_current_ctx: "Context | None" = None # 注入当前进程上下文
|
||||||
|
|
||||||
|
|
||||||
class SubProcess:
|
class SubProcess:
|
||||||
def __init__(
|
def __init__(
|
||||||
self, name: str, func: ProcessFuncType, ctx: Context = Context, *args, **kwargs
|
self,
|
||||||
|
name: str,
|
||||||
|
func: ProcessFuncType,
|
||||||
|
ctx: Context = Context(),
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
):
|
):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.func = func
|
self.func = func
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ from croterline.process import SubProcess, get_ctx
|
|||||||
|
|
||||||
|
|
||||||
def p_func(*args, **kwargs):
|
def p_func(*args, **kwargs):
|
||||||
|
print("Args", args)
|
||||||
|
print("Kwargs", kwargs)
|
||||||
i = 0
|
i = 0
|
||||||
ctx = get_ctx()
|
ctx = get_ctx()
|
||||||
while True:
|
while True:
|
||||||
@@ -15,10 +17,16 @@ def p_func(*args, **kwargs):
|
|||||||
ctx.sub_chan << "end"
|
ctx.sub_chan << "end"
|
||||||
|
|
||||||
|
|
||||||
|
def p_func2(*args, **kwargs):
|
||||||
|
print("args: ", args)
|
||||||
|
print("kwargs: ", kwargs)
|
||||||
|
raise Exception("Test")
|
||||||
|
|
||||||
|
|
||||||
class TestSubProcess:
|
class TestSubProcess:
|
||||||
def test_run(self):
|
def test_run(self):
|
||||||
print("start")
|
print("start")
|
||||||
sp = SubProcess("test", p_func, Context())
|
sp = SubProcess("test", p_func, Context(), 1, 2, 3, k1=1, k2=2)
|
||||||
sp.start()
|
sp.start()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@@ -30,5 +38,7 @@ class TestSubProcess:
|
|||||||
print("finished")
|
print("finished")
|
||||||
sp.terminate()
|
sp.terminate()
|
||||||
|
|
||||||
def test_decorator(self):
|
def test_input(self):
|
||||||
pass
|
print("test_input")
|
||||||
|
sp = SubProcess("test2", p_func2, Context(), 1, 2, 3, host=1, port=2)
|
||||||
|
sp.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user