Skip to content

Commit

Permalink
✨ first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
snowykami committed Oct 12, 2024
1 parent 5242efe commit a2f07b7
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Publish

on:
push:
tags:
- 'v*'

jobs:
pypi-publish:
name: upload release to PyPI
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v3

- uses: pdm-project/setup-pdm@v3

- name: Publish package distributions to PyPI
run: pdm publish
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm-project.org/#use-with-ide
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
4 changes: 4 additions & 0 deletions croterline/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from multiprocessing import set_start_method

# 设置Linux下默认开新进程的方式
set_start_method("spawn", force=True)
15 changes: 15 additions & 0 deletions croterline/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Callable, Any

from magicoca.chan import Chan


class Context:
def __init__(self):
self.main_chan: Chan[Any] = Chan[Any]() # main to sub
self.sub_chan: Chan[Any] = Chan[Any]() # sub to main

def set_value(self, key: str, value: Any):
setattr(self, key, value)

def get_value(self, key: str) -> Any:
return getattr(self, key)
87 changes: 87 additions & 0 deletions croterline/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from multiprocessing import Process as _Process
from typing import Callable, Any

from croterline.context import Context
from croterline.utils import IsMainProcess

type ProcessFuncType = Callable[[tuple[Any, ...], dict[str, Any]], None]

_processes: dict[str, "SubProcess"] = {}

_current_ctx: "Context | None" = None # 注入当前进程上下文


class SubProcess:
def __init__(
self, name: str, func: ProcessFuncType, ctx: Context = Context, *args, **kwargs
):
self.name = name
self.func = func
self.ctx = ctx
self.args = args
self.kwargs = kwargs

self.process: _Process | None = None

def start(self):
self.process = _Process(
target=_wrapper,
args=(self.func, self.ctx, *self.args),
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:
"""
获取进程上下文,在主进程中调用需指定进程名称,若在子进程中调用则无需指定进程名称
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


def _wrapper(func: ProcessFuncType, ctx: Context, *args, **kwargs):
global _current_ctx

_current_ctx = ctx
func(*args, **kwargs)
3 changes: 3 additions & 0 deletions croterline/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from multiprocessing import current_process

IsMainProcess = current_process().name == "MainProcess"
Loading

0 comments on commit a2f07b7

Please sign in to comment.