Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial idea for global channel environment #173

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:

- run: |
mk python-release owner=vkottler \
repo=runtimepy version=3.7.6
repo=runtimepy version=4.0.0
if: |
matrix.python-version == '3.11'
&& matrix.system == 'ubuntu-latest'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.4
hash=5e658f86474b5fde932a9ad02e57bb7b
hash=0eae1fb0651f9608411daf093deabfbc
=====================================
-->

# runtimepy ([3.7.6](https://pypi.org/project/runtimepy/))
# runtimepy ([4.0.0](https://pypi.org/project/runtimepy/))

[![python](https://img.shields.io/pypi/pyversions/runtimepy.svg)](https://pypi.org/project/runtimepy/)
![Build Status](https://github.com/vkottler/runtimepy/workflows/Python%20Package/badge.svg)
Expand Down
6 changes: 3 additions & 3 deletions local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 3
minor: 7
patch: 6
major: 4
minor: 0
patch: 0
entry: runtimepy
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "runtimepy"
version = "3.7.6"
version = "4.0.0"
description = "A framework for implementing Python services."
readme = "README.md"
requires-python = ">=3.11"
Expand Down
4 changes: 2 additions & 2 deletions runtimepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.4
# hash=f807afa762f8662fce8e0c03e04b39d8
# hash=372c7409fed9799f8307142ddbc1c739
# =====================================

"""
Expand All @@ -10,7 +10,7 @@

DESCRIPTION = "A framework for implementing Python services."
PKG_NAME = "runtimepy"
VERSION = "3.7.6"
VERSION = "4.0.0"

# runtimepy-specific content.
METRICS_NAME = "metrics"
12 changes: 8 additions & 4 deletions runtimepy/channel/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
A module implementing a channel environment.
"""

# built-in
from typing import Iterator as _Iterator

# internal
from runtimepy.channel.environment.array import (
ArrayChannelEnvironment as _ArrayChannelEnvironment,
Expand All @@ -12,15 +15,16 @@
from runtimepy.channel.environment.file import (
FileChannelEnvironment as _FileChannelEnvironment,
)
from runtimepy.channel.environment.names import (
ChannelNameEnvironment as _ChannelNameEnvironment,
)


class ChannelEnvironment(
_ArrayChannelEnvironment,
_FileChannelEnvironment,
_CreateChannelEnvironment,
_ChannelNameEnvironment,
):
"""A class integrating channel and enumeration registries."""

@property
def names(self) -> _Iterator[str]:
"""Iterate over registered names in the environment."""
yield from self.channels.names.names
20 changes: 0 additions & 20 deletions runtimepy/channel/environment/names.py

This file was deleted.

55 changes: 55 additions & 0 deletions runtimepy/channel/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# built-in
from typing import Any as _Any
from typing import NamedTuple
from typing import Optional as _Optional
from typing import Type as _Type
from typing import Union
Expand All @@ -29,6 +30,46 @@ class ChannelNameRegistry(_NameRegistry):
name_regex = _CHANNEL_PATTERN


class ChannelCreation(NamedTuple):
"""A container for channel-creation parameters."""

name: str
kind: Union[Primitive[_Any], _Primitivelike]
commandable: bool = False
enum: _Optional[_RegistryKey] = None
scaling: _Optional[ChannelScaling] = None


class GlobalEnvironment:
"""A global environment management interface."""

registry: "ChannelRegistry"

def __init__(self) -> None:
"""Initialize this instance."""

self.duplicates: list[tuple[_AnyChannel, ChannelCreation]] = []

# We should add a 'num_duplicates' channel, maybe 'num_channels' as
# well?

def handle(self, channel: _AnyChannel, meta: ChannelCreation) -> None:
"""Handle global channel registration (used for instrumentation)."""

if not self.registry.channel(
meta.name,
meta.kind,
commandable=meta.commandable,
enum=meta.enum,
scaling=meta.scaling,
):
self.duplicates.append((channel, meta))


# Keep track of global channels.
GLOBAL: GlobalEnvironment = GlobalEnvironment()


class ChannelRegistry(_Registry[_Channel[_Any]]):
"""A runtime enumeration registry."""

Expand Down Expand Up @@ -77,4 +118,18 @@ def channel(
if result is not None:
result.raw = primitive

GLOBAL.handle(
result,
ChannelCreation(
name,
kind,
commandable=commandable,
enum=enum,
scaling=scaling,
),
)

return result


GLOBAL.registry = ChannelRegistry.create()
3 changes: 1 addition & 2 deletions runtimepy/net/arbiter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ async def _entry(
"""

result = -1
info: Optional[AppInfo] = None

try:
# Wait for servers to start.
Expand Down Expand Up @@ -211,8 +212,6 @@ async def _entry(
for name, conn in self._connections.items():
register_env(name, conn.command)

info: Optional[AppInfo] = None

# Run application, but only if all the registered connections are
# still alive after initialization.
if not check_connections or not any(
Expand Down
16 changes: 10 additions & 6 deletions runtimepy/net/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# third-party
from vcorelib.asyncio import log_exceptions as _log_exceptions
from vcorelib.logging import LoggerMixin
from vcorelib.math import default_time_ns as _default_time_ns

# internal
Expand All @@ -22,11 +23,13 @@
T = _TypeVar("T", bound=_Connection)


class ConnectionManager:
class ConnectionManager(LoggerMixin):
"""A class for managing connection processing at runtime."""

def __init__(self) -> None:
"""Initialize this connection manager."""

super().__init__()
self.queue: _asyncio.Queue[_Connection] = _asyncio.Queue()
self._running = False
self._conns: _List[_Connection] = []
Expand Down Expand Up @@ -113,10 +116,11 @@ async def manage(self, stop_sig: _asyncio.Event) -> None:

tasks = next_tasks

# Allow existing tasks to clean up.
if new_conn_task is not None:
new_conn_task.cancel()
for task in tasks:
await task
with self.log_time("Shutting down", reminder=True):
# Allow existing tasks to clean up.
if new_conn_task is not None:
new_conn_task.cancel()
for task in tasks:
await task

self._running = False
Loading