Skip to main content

Classic Signals

Библиотека предоставляет просую реализацию паттерна Observer для инверсии контроля через сигналы и реакции.

Потокобезопасно.

В одном месте программы мы объявляем сигналы, пользуясь декоратором signal, в других местах приложения пишем реакции на этих сигналов. Реакции - это любой вызываемый объект, принимающий один аргумент - инстанс сигнала. Это может быть и просто функция или метод. Последнее, что нужно, это инстанс класса Hub. В Hub перед началом работы можно зарегистрировать реакции, потом можно вызвать у него метод notify, передав в него инстанс сигнала, и Hub вызовет все реакции, относящиеся к сигналу.

Реакции исполняются последовательно в единственном текущем потоке. Не рекомендуется создавать новые потоки из сигналов. Этот механизм предназначается для связки разных частей кода с инверсией зависимости, не для распараллеливания задач.

Пример:

from dataclasses import dataclass

from classic.signals import Hub


@dataclass
class SomethingHappened:
    some_field: int


@dataclass
class SomethingAnotherHappened:
    some_field: int


@dataclass
class SomeHandlers:
    hub: Hub
    
    def __post_init__(self):
        self.hub.add_reaction(
            SomethingHappened,
            self.on_something_happened,
        )
        self.hub.add_reaction(
            SomethingAnotherHappened,
            self.on_something_another_happened,
        )
    
    def on_something_happened(self, signal: SomethingHappened):
        print(f'Что-то произошло: {signal}')
    
    def on_something_another_happened(self, signal: SomethingAnotherHappened):
        print(f'Что-то еще произошло: {signal}')


hub = Hub()
SomeHandlers(hub=hub)

signal_1 = SomethingHappened(some_field=0)
signal_2 = SomethingAnotherHappened(some_field=0)

hub.notify(signal_1, signal_2)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

classic_signals-1.0.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

classic_signals-1.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file classic_signals-1.0.0.tar.gz.

File metadata

  • Download URL: classic_signals-1.0.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for classic_signals-1.0.0.tar.gz
Algorithm Hash digest
SHA256 dbec4524462bfde2c823327cc248823b6249c11df811a62a2dfaab6d86177bde
MD5 a7baf31fe02c8ffd6e29535bb59290b9
BLAKE2b-256 b9e55566e51f7e1a165dd631736e81ee354146a65ecfc5e5dbc754761a777eed

See more details on using hashes here.

Provenance

The following attestation bundles were made for classic_signals-1.0.0.tar.gz:

Publisher: publish.yml on variasov/classic-signals

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file classic_signals-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for classic_signals-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c90fa757b301ef60375ac20c68a736e6cf02f6aa62e7080fa048c3bb85bb7c80
MD5 97f82f8cc477385a1f6cd0ad52df2e53
BLAKE2b-256 e8184393af3b0fb505d86382633314a0beab2106554a61df507c2df3506f3c9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for classic_signals-1.0.0-py3-none-any.whl:

Publisher: publish.yml on variasov/classic-signals

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

0.1.1

2 files

0.1.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page