mirror of
https://github.com/LiteyukiStudio/croterline.git
synced 2024-11-25 10:05:06 +08:00
✨ first commit
This commit is contained in:
parent
a2f07b7e6f
commit
5cb3424ad0
@ -6,8 +6,6 @@ from croterline.utils import IsMainProcess
|
||||
|
||||
type ProcessFuncType = Callable[[tuple[Any, ...], dict[str, Any]], None]
|
||||
|
||||
_processes: dict[str, "SubProcess"] = {}
|
||||
|
||||
_current_ctx: "Context | None" = None # 注入当前进程上下文
|
||||
|
||||
|
||||
@ -20,7 +18,6 @@ class SubProcess:
|
||||
self.ctx = ctx
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
|
||||
self.process: _Process | None = None
|
||||
|
||||
def start(self):
|
||||
@ -30,54 +27,25 @@ class SubProcess:
|
||||
kwargs=self.kwargs,
|
||||
)
|
||||
self.process.start()
|
||||
set_process(self.name, self)
|
||||
|
||||
def terminate(self):
|
||||
self.process.terminate()
|
||||
set_process(self.name, None)
|
||||
|
||||
def join(self, timeout: float = 0):
|
||||
self.process.join(timeout=timeout)
|
||||
set_process(self.name, None)
|
||||
|
||||
|
||||
def set_process(name: str, process: SubProcess | 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:
|
||||
def get_ctx() -> Context | None:
|
||||
"""
|
||||
获取进程上下文,在主进程中调用需指定进程名称,若在子进程中调用则无需指定进程名称
|
||||
Returns:
|
||||
进程上下文
|
||||
"""
|
||||
if name is not None:
|
||||
if not IsMainProcess:
|
||||
raise RuntimeError(
|
||||
"get_ctx with specific name can only be called in the main process."
|
||||
)
|
||||
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
|
||||
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):
|
||||
|
34
pdm.lock
34
pdm.lock
@ -5,7 +5,7 @@
|
||||
groups = ["default", "dev"]
|
||||
strategy = ["inherit_metadata"]
|
||||
lock_version = "4.5.0"
|
||||
content_hash = "sha256:4affe52c3e672fd14a8c7a461b5a250cda491156c7673dd316492dc40f5e2512"
|
||||
content_hash = "sha256:ff85cbea1723172b9a262b551ca55c7c607b88598545a4c8ec8e489bf7be8355"
|
||||
|
||||
[[metadata.targets]]
|
||||
requires_python = ">=3.10"
|
||||
@ -107,6 +107,37 @@ files = [
|
||||
{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]]
|
||||
name = "mypy-extensions"
|
||||
version = "1.0.0"
|
||||
@ -199,7 +230,6 @@ version = "4.12.2"
|
||||
requires_python = ">=3.8"
|
||||
summary = "Backported and Experimental Type Hints for Python 3.8+"
|
||||
groups = ["dev"]
|
||||
marker = "python_version < \"3.11\""
|
||||
files = [
|
||||
{file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"},
|
||||
{file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"},
|
||||
|
@ -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 = [
|
||||
"pytest>=8.3.3",
|
||||
"black>=24.10.0",
|
||||
"mypy>=1.11.2",
|
||||
]
|
||||
[tool.pdm]
|
||||
distribution = true
|
||||
|
Loading…
Reference in New Issue
Block a user