TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Reactive Signals for Python – inspired by Angular's reactivity model

31 pointsby buibuibui4 months ago
Hey everyone, I built reaktiv, a small reactive signals library for Python, inspired by Angular’s reactivity model. It lets you define Signals, Computed Values, and Effects that automatically track dependencies and update efficiently. The main focus is async-first reactivity without external dependencies.<p>Here is an example code:<p>``` import asyncio from reaktiv import Signal, ComputeSignal, Effect<p>async def main(): count = Signal(0) doubled = ComputeSignal(lambda: count.get() * 2)<p><pre><code> async def log_count(): print(f&quot;Count: {count.get()}, Doubled: {doubled.get()}&quot;) Effect(log_count).schedule() count.set(5) # Triggers: &quot;Count: 5, Doubled: 10&quot; await asyncio.sleep(0) # Allow effects to process </code></pre> asyncio.run(main()) ```

5 comments

philote4 months ago
For those of use that haven&#x27;t use Angular, can you explain what this is doing? And what&#x27;s it do that we can&#x27;t already do with asyncio? Also, it&#x27;s weird you have to call `asyncio.sleep(0)`. Seems like you should be able to `await count.set(5)`.<p>I did look at the repo, but I couldn&#x27;t easily understand the use case and what exactly it&#x27;s doing from examples alone.
评论 #42881884 未加载
jitl4 months ago
What kinds of Python programs would benefit from Signals? Most of the Python I&#x27;ve written is either small CLI tools that run to completion, or boring web servers using frameworks like Django that are request-response oriented, which also has a &quot;run-to-completion&quot; model for a request&#x27;s handler code.<p>Also, I looked at your implementation, and your ComputedSignal implementation is not sound - it can give inconsistent results with respect to the input signals depending on the ordering of reads when you have a ComputedSignal that depends on other ComputedSignal.
评论 #42884811 未加载
评论 #42882266 未加载
fermigier4 months ago
Cool (probably), but it&#x27;s not clear what it does or how useful it is when one doesn&#x27;t know anything about Angular signals. Could you explain?<p>Also, care to compare to RxPY? (<a href="https:&#x2F;&#x2F;github.com&#x2F;ReactiveX&#x2F;RxPY">https:&#x2F;&#x2F;github.com&#x2F;ReactiveX&#x2F;RxPY</a>)
评论 #42882372 未加载
评论 #42897383 未加载
buibuibui4 months ago
I don’t know how to edit my submission, but after reading the comments, I realize the example code didn’t do a good job of demonstrating what my code does. Here’s a more verbose example showing a stock ticker:<p><pre><code> 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&#x27;s portfolio shares = Signal({ &#x27;AAPL&#x27;: 100, &#x27;GOOGL&#x27;: 50 }) # Computed total portfolio value portfolio_value = ComputeSignal(lambda: ( shares.get()[&#x27;AAPL&#x27;] * apple_price.get() + shares.get()[&#x27;GOOGL&#x27;] * google_price.get() )) # Price alert system async def check_alerts(): if apple_price.get() &gt; 200: print(&quot;AAPL alert: Above $200!&quot;) if google_price.get() &lt; 2700: print(&quot;GOOGL alert: Below $2700!&quot;) # 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&quot;AAPL: ${apple_price.get():,.2f} GOOGL: ${google_price.get():,.2f}&quot;) # Track portfolio value async def monitor_portfolio(): print(f&quot;Current value: ${portfolio_value.get():,.2f}&quot;) # 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()) </code></pre> Example output:<p><pre><code> 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!</code></pre>
throwaway3141554 months ago
HN doesn&#x27;t support markdown unfortunately. As such your code block&#x27;s formatting is all messed up there.<p>Not your fault of course. Just thought you&#x27;d wanna know.<p>P.S. - HN staff please add markdown support. I&#x27;m sure this gets mentioned 10 times a day, so this is mention 11.
评论 #42893489 未加载