159 karma · joined July 2, 2023
The Signals evaluation are topologically ordered and are running synchronously. Effects are running in the order they are defined.
The frontend ecosystem has already embraced Signal-based reactivity (Angular, SolidJS, React) with tremendous success. It's time for Python developers to enjoy these same benefits. My breakthrough came when I stopped describing it as "Angular signals for Python" and started calling it "Excel-like computation graphs" - suddenly Python developers understood the value.
reaktiv automatically tracks dependencies between computations, only recalculating values when dependencies change. This eliminates manual dependency tracking, prevents synchronization bugs, and reduces cognitive load when managing complex state - benefits that are just as valuable on the backend as they are in UI frameworks.
I'm seeing initial interest from data scientists and believe the Home Assistant/IoT communities could benefit greatly from this approach. Python deserves the same modern reactivity patterns that have revolutionized frontend development.
Would love your thoughts on bringing Signals to the Python ecosystem!
The frontend ecosystem has already embraced Signal-based reactivity (Angular, SolidJS, React) with tremendous success. It's time for Python developers to enjoy these same benefits. My breakthrough came when I stopped describing it as "Angular signals for Python" and started calling it "Excel-like computation graphs" - suddenly Python developers understood the value.
reaktiv automatically tracks dependencies between computations, only recalculating values when dependencies change. This eliminates manual dependency tracking, prevents synchronization bugs, and reduces cognitive load when managing complex state - benefits that are just as valuable on the backend as they are in UI frameworks.
I'm seeing initial interest from data scientists and believe the Home Assistant/IoT communities could benefit greatly from this approach. Python deserves the same modern reactivity patterns that have revolutionized frontend development.
Would love your thoughts on bringing Signals to the Python ecosystem!
import asyncio
from reaktiv import Signal, ComputeSignal, Effect
async def main():
# Real-time stock prices
apple_price = Signal(195.00)
google_price = Signal(2750.00)
# User's portfolio
shares = Signal({
'AAPL': 100,
'GOOGL': 50
})
# Computed total portfolio value
portfolio_value = ComputeSignal(lambda: (
shares.get()['AAPL'] * apple_price.get() +
shares.get()['GOOGL'] * google_price.get()
))
# Price alert system
async def check_alerts():
if apple_price.get() > 200:
print("AAPL alert: Above $200!")
if google_price.get() < 2700:
print("GOOGL alert: Below $2700!")
# Automatic updates
async def live_updates():
while True:
await asyncio.sleep(1)
apple_price.set(apple_price.get() * 1.01) # +1%
google_price.set(google_price.get() * 0.995) # -0.5%
print(f"AAPL: ${apple_price.get():,.2f} GOOGL: ${google_price.get():,.2f}")
# Track portfolio value
async def monitor_portfolio():
print(f"Current value: ${portfolio_value.get():,.2f}")
# Set up effects
alerts_effect = Effect(check_alerts)
updates_effect = Effect(live_updates)
portfolio_effect = Effect(monitor_portfolio)
alerts_effect.schedule()
updates_effect.schedule()
portfolio_effect.schedule()
# Run for 5 seconds
await asyncio.sleep(5)
asyncio.run(main())
Example output: Current value: $157,000.00
AAPL: $196.95 GOOGL: $2,736.25
Current value: $156,507.50
AAPL: $198.92 GOOGL: $2,722.57
Current value: $156,020.39
AAPL: $200.91 GOOGL: $2,708.96
AAPL alert: Above $200!
Current value: $155,538.66
AAPL: $202.92 GOOGL: $2,695.41
AAPL alert: Above $200!
GOOGL alert: Below $2700!RxPY is a full reactive programming toolkit with observables, operators, and complex stream processing, while Signals are a more lightweight and declarative way to track state dependencies. RxPY is great for handling event streams, but for simple state management and reactive updates, Signals are much easier to reason about.
Could you share an example of the problem you’re referring to?
In frontend frameworks, Signals eliminate the need for manual subscriptions and event handling, making state updates more efficient. I wanted the same benefits for backend systems where changes to shared state should propagate automatically, without polling, callbacks, or race conditions.
For example, if a config value or cached result updates, anything depending on it should react immediately. With Signals, you don’t have to manually notify consumers or keep track of what depends on what. It just works (hopefully).
About `asyncio.sleep(0)`: it’s just there to keep the example concise. Normally, you’d be inside an async function that naturally yields control, like handling a request or waiting for I/O.