Skip to main content

A simple "untwisted" approach to event-driven programming

Project description

Whether it’s an application server or a desktop application, any sufficiently complex system is event-driven – and that usually means callbacks.

Unfortunately, explicit callback management is to event-driven programming what explicit memory management is to most other kinds of programming: a tedious hassle and a significant source of unnecessary bugs.

For example, even in a single-threaded program, callbacks can create race conditions, if the callbacks are fired in an unexpected order. If a piece of code can cause callbacks to be fired “in the middle of something”, both that code and the callbacks can get confused.

Of course, that’s why most GUI libraries and other large event-driven systems usually have some way for you to temporarily block callbacks from happening. This lets you fix or workaround your callback order dependency bugs… at the cost of adding even more tedious callback management. And it still doesn’t fix the problem of forgetting to cancel callbacks… or register needed ones in the first place!

The Trellis solves all of these problems by introducing automatic callback management, in much the same way that Python does automatic memory management. Instead of worrying about subscribing or “listening” to events and managing the order of callbacks, you just write rules to compute values. The Trellis “sees” what values your rules access, and thus knows what rules may need to be rerun when something changes – not unlike the operation of a spreadsheet.

But even more important, it also ensures that callbacks can’t happen while code is “in the middle of something”. Any action a rule takes that would cause a new event to fire is automatically deferred until all of the applicable rules have had a chance to respond to the event(s) in progress. And, if you try to access the value of a rule that hasn’t been updated yet, it’s automatically updated on-the-fly so that it reflects the current event in progress.

No stale data. No race conditions. No callback management. That’s what the Trellis gives you.

Here’s a super-trivial example:

>>> from peak.events import trellis

>>> class TempConverter(trellis.Component):
...     trellis.values(
...         F = 32,
...         C = 0,
...     )
...     trellis.rules(
...         F = lambda self: self.C * 1.8 + 32,
...         C = lambda self: (self.F - 32)/1.8,
...     )
...     @trellis.action
...     def show_values(self):
...         print "Celsius......", self.C
...         print "Fahrenheit...", self.F

>>> tc = TempConverter(C=100)
Celsius...... 100
Fahrenheit... 212.0

>>> tc.F = 32
Celsius...... 0.0
Fahrenheit... 32

>>> tc.C = -40
Celsius...... -40
Fahrenheit... -40.0

As you can see, each attribute is updated if the other one changes, and the show_values action is invoked any time the dependent values change… but not if they don’t:

>>> tc.C = -40

Since the value didn’t change, none of the rules based on it were recalculated.

Now, imagine all this, but scaled up to include rules that can depend on things like how long it’s been since something happened… whether a mouse button was clicked… whether a socket is readable… or whether a Twisted “deferred” object has fired. With automatic dependency tracking that spans function calls, so you don’t even need to know what values your rule depends on, let alone having to explicitly code any dependencies in!

Imagine painless MVC, where you simply write rules like the above to update GUI widgets with application values… and vice versa.

And then, you’ll have the tiny beginning of a mere glimpse… of what the Trellis can do for you.

Other Python libraries exist which attempt to do similar things, of course; PyCells and Cellulose are two. However, only the Trellis supports fully circular rules (like the temperature conversion example above), and intra-pulse write conflict detection. The Trellis also uses less memory for each cell (rule/value object), and offers many other features that either PyCells or Cellulose lack.

The Trellis also boasts an extensive Tutorial and Reference Manual, and can be downloaded from the Python Package Index or installed using Easy Install.

Questions, discussion, and bug reports for the Trellis should be directed to the PEAK mailing list.

Project details


Download files

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

Source Distribution

Trellis-0.5b1.zip (65.7 kB view details)

Uploaded Source

Built Distributions

Trellis-0.5b1-py2.5.egg (41.6 kB view details)

Uploaded Source

Trellis-0.5b1-py2.4.egg (42.6 kB view details)

Uploaded Source

Trellis-0.5b1-py2.3.egg (157.3 kB view details)

Uploaded Source

File details

Details for the file Trellis-0.5b1.zip.

File metadata

  • Download URL: Trellis-0.5b1.zip
  • Upload date:
  • Size: 65.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Trellis-0.5b1.zip
Algorithm Hash digest
SHA256 f28e629edcfe22c5462a95faa158f120da33559c70d1beafdf1ef711d0a04905
MD5 83ab98091064386d70dc23713d1ff3e1
BLAKE2b-256 4055d35176a049df0f3d15c81455a403d31cff0458586dffcad022a858dc4c5e

See more details on using hashes here.

File details

Details for the file Trellis-0.5b1-py2.5.egg.

File metadata

  • Download URL: Trellis-0.5b1-py2.5.egg
  • Upload date:
  • Size: 41.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Trellis-0.5b1-py2.5.egg
Algorithm Hash digest
SHA256 0f4b815494d88bc6f3aa3a185060c2e68c5ecfa41c8e6300176d0a6c3f3da474
MD5 814f3a8509916c6ca0b2e5ef7ea3f4f3
BLAKE2b-256 97abeb419434319bcd8b7f3536706d36d4265b699ac4c425752a123bc7a2ad99

See more details on using hashes here.

File details

Details for the file Trellis-0.5b1-py2.4.egg.

File metadata

  • Download URL: Trellis-0.5b1-py2.4.egg
  • Upload date:
  • Size: 42.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Trellis-0.5b1-py2.4.egg
Algorithm Hash digest
SHA256 9e41d3ada5092e3dfb8bec11df527e0965cf6c0f94e0765ee096ae6f54f9a36d
MD5 cb8d395001fcd4fa8084ec7817835b1f
BLAKE2b-256 9aae3cf2019f91368d824e9e8d1bcf681263877f3b68d1a2bfe7aad5e58873de

See more details on using hashes here.

File details

Details for the file Trellis-0.5b1-py2.3.egg.

File metadata

  • Download URL: Trellis-0.5b1-py2.3.egg
  • Upload date:
  • Size: 157.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Trellis-0.5b1-py2.3.egg
Algorithm Hash digest
SHA256 051a73abe14d7fb674da39ba47bb4d56409305a645bce6822b268e862420711d
MD5 9e5248acf3bad385df52743b09f77406
BLAKE2b-256 99601274c1f624dc449a6d609e75c1135be0610d075ad0fea5931ebaad9ee941

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page