3 Commits

Author SHA1 Message Date
9c50844195 🐛 remove debug output 2024-10-13 23:56:00 +08:00
e151a7af0c first commit 2024-10-13 17:28:37 +08:00
5cb3424ad0 first commit 2024-10-13 00:12:24 +08:00
4 changed files with 55 additions and 44 deletions

View File

@@ -1,12 +1,12 @@
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]
_processes: dict[str, "SubProcess"] = {}
_current_ctx: "Context | None" = None # 注入当前进程上下文 _current_ctx: "Context | None" = None # 注入当前进程上下文
@@ -20,7 +20,6 @@ class SubProcess:
self.ctx = ctx self.ctx = ctx
self.args = args self.args = args
self.kwargs = kwargs self.kwargs = kwargs
self.process: _Process | None = None self.process: _Process | None = None
def start(self): def start(self):
@@ -30,54 +29,25 @@ class SubProcess:
kwargs=self.kwargs, kwargs=self.kwargs,
) )
self.process.start() self.process.start()
set_process(self.name, self)
def terminate(self): def terminate(self):
self.process.terminate() self.process.terminate()
set_process(self.name, None)
def join(self, timeout: float = 0): def join(self, timeout: float = 0):
self.process.join(timeout=timeout) self.process.join(timeout=timeout)
set_process(self.name, None)
def set_process(name: str, process: SubProcess | None): def get_ctx() -> Context | None:
_processes[name] = process
def get_process(name: str) -> SubProcess | None:
"""
获取进程对象,在主进程中调用,只可在主进程调用
Args:
name: 进程名称
Returns:
进程对象
"""
if not IsMainProcess:
raise RuntimeError(
"get_process with specific name can only be called in the main process."
)
return _processes.get(name, None)
def get_ctx(name: str | None = None) -> Context | None:
""" """
获取进程上下文,在主进程中调用需指定进程名称,若在子进程中调用则无需指定进程名称 获取进程上下文,在主进程中调用需指定进程名称,若在子进程中调用则无需指定进程名称
Returns: Returns:
进程上下文 进程上下文
""" """
if name is not None: if IsMainProcess:
if not IsMainProcess: raise RuntimeError(
raise RuntimeError( "get_ctx without name can only be called in the sub process."
"get_ctx with specific name can only be called in the main process." )
) return _current_ctx
return _processes.get(name, None).ctx
else:
if IsMainProcess:
raise RuntimeError(
"get_ctx without name can only be called in the sub process."
)
return _current_ctx
def _wrapper(func: ProcessFuncType, ctx: Context, *args, **kwargs): def _wrapper(func: ProcessFuncType, ctx: Context, *args, **kwargs):

34
pdm.lock generated
View File

@@ -5,7 +5,7 @@
groups = ["default", "dev"] groups = ["default", "dev"]
strategy = ["inherit_metadata"] strategy = ["inherit_metadata"]
lock_version = "4.5.0" lock_version = "4.5.0"
content_hash = "sha256:4affe52c3e672fd14a8c7a461b5a250cda491156c7673dd316492dc40f5e2512" content_hash = "sha256:ff85cbea1723172b9a262b551ca55c7c607b88598545a4c8ec8e489bf7be8355"
[[metadata.targets]] [[metadata.targets]]
requires_python = ">=3.10" requires_python = ">=3.10"
@@ -107,6 +107,37 @@ files = [
{file = "magicoca-1.0.1.tar.gz", hash = "sha256:0dbc9a35609db92ec79076f7126566c1e71bd4b853909ecbad9221dcc7fd6f31"}, {file = "magicoca-1.0.1.tar.gz", hash = "sha256:0dbc9a35609db92ec79076f7126566c1e71bd4b853909ecbad9221dcc7fd6f31"},
] ]
[[package]]
name = "mypy"
version = "1.11.2"
requires_python = ">=3.8"
summary = "Optional static typing for Python"
groups = ["dev"]
dependencies = [
"mypy-extensions>=1.0.0",
"tomli>=1.1.0; python_version < \"3.11\"",
"typing-extensions>=4.6.0",
]
files = [
{file = "mypy-1.11.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d42a6dd818ffce7be66cce644f1dff482f1d97c53ca70908dff0b9ddc120b77a"},
{file = "mypy-1.11.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:801780c56d1cdb896eacd5619a83e427ce436d86a3bdf9112527f24a66618fef"},
{file = "mypy-1.11.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:41ea707d036a5307ac674ea172875f40c9d55c5394f888b168033177fce47383"},
{file = "mypy-1.11.2-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:6e658bd2d20565ea86da7d91331b0eed6d2eee22dc031579e6297f3e12c758c8"},
{file = "mypy-1.11.2-cp310-cp310-win_amd64.whl", hash = "sha256:478db5f5036817fe45adb7332d927daa62417159d49783041338921dcf646fc7"},
{file = "mypy-1.11.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:75746e06d5fa1e91bfd5432448d00d34593b52e7e91a187d981d08d1f33d4385"},
{file = "mypy-1.11.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:a976775ab2256aadc6add633d44f100a2517d2388906ec4f13231fafbb0eccca"},
{file = "mypy-1.11.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:cd953f221ac1379050a8a646585a29574488974f79d8082cedef62744f0a0104"},
{file = "mypy-1.11.2-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:57555a7715c0a34421013144a33d280e73c08df70f3a18a552938587ce9274f4"},
{file = "mypy-1.11.2-cp311-cp311-win_amd64.whl", hash = "sha256:36383a4fcbad95f2657642a07ba22ff797de26277158f1cc7bd234821468b1b6"},
{file = "mypy-1.11.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:e8960dbbbf36906c5c0b7f4fbf2f0c7ffb20f4898e6a879fcf56a41a08b0d318"},
{file = "mypy-1.11.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:06d26c277962f3fb50e13044674aa10553981ae514288cb7d0a738f495550b36"},
{file = "mypy-1.11.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:6e7184632d89d677973a14d00ae4d03214c8bc301ceefcdaf5c474866814c987"},
{file = "mypy-1.11.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:3a66169b92452f72117e2da3a576087025449018afc2d8e9bfe5ffab865709ca"},
{file = "mypy-1.11.2-cp312-cp312-win_amd64.whl", hash = "sha256:969ea3ef09617aff826885a22ece0ddef69d95852cdad2f60c8bb06bf1f71f70"},
{file = "mypy-1.11.2-py3-none-any.whl", hash = "sha256:b499bc07dbdcd3de92b0a8b29fdf592c111276f6a12fe29c30f6c417dd546d12"},
{file = "mypy-1.11.2.tar.gz", hash = "sha256:7f9993ad3e0ffdc95c2a14b66dee63729f021968bff8ad911867579c65d13a79"},
]
[[package]] [[package]]
name = "mypy-extensions" name = "mypy-extensions"
version = "1.0.0" version = "1.0.0"
@@ -199,7 +230,6 @@ version = "4.12.2"
requires_python = ">=3.8" requires_python = ">=3.8"
summary = "Backported and Experimental Type Hints for Python 3.8+" summary = "Backported and Experimental Type Hints for Python 3.8+"
groups = ["dev"] groups = ["dev"]
marker = "python_version < \"3.11\""
files = [ files = [
{file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"}, {file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"},
{file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"},

View File

@@ -25,6 +25,7 @@ tag_regex = '^v(?:\D*)?(?P<version>([1-9][0-9]*!)?(0|[1-9][0-9]*)(\.(0|[1-9][0-9
dev = [ dev = [
"pytest>=8.3.3", "pytest>=8.3.3",
"black>=24.10.0", "black>=24.10.0",
"mypy>=1.11.2",
] ]
[tool.pdm] [tool.pdm]
distribution = true distribution = true

View File

@@ -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()