pynetevents
A lightweight, flexible C#-like event system in Python supporting strong/weak references, async listeners, and exception propagation.
Features
- Event slots — attach and fire multiple listeners
- Descriptor-based events
- Supports sync and async listeners
- Weak reference support to avoid memory leaks
- Duplicate listener protection
- Exception propagation
- Clean syntax:
+=/-=
Table of Contents
Check the API Reference for full details.
Quick Start
import asyncio
from pynetevents import Event
class ChatServer:
on_message = Event()
def send_message(self, msg):
self.on_message(msg)
def log(msg):
print(f"[LOG] {msg}")
async def save(msg):
await asyncio.sleep(0.1)
print(f"[ASYNC] Saved {msg}")
server = ChatServer()
server.on_message += log
server.on_message += save
server.send_message("Hello World")
EventSlots
EventSlot and EventSlotWeakRef are containers for listeners.
Use += / -= to subscribe/unsubscribe.
EventSlot
Strong references, suitable for long-lived listeners:
from pynetevents import EventSlot
slot = EventSlot("on_data")
slot += lambda x: print(f"Received {x}")
slot("Test")
EventSlotWeakRef
Weak references, automatically cleared when listener is deleted:
from pynetevents import EventSlotWeakRef
class Listener:
def on_event(self, msg):
print(msg)
slot = EventSlotWeakRef("on_message")
listener = Listener()
slot += listener.on_event
slot("Hello")
del listener
slot("World") # No output
Common Usage
subscribe,subscribe_weak,unsubscribe,unsubscribe_weakexist, but+=/-=is simpler.invoke()calls sync listeners and schedules async (fire-and-forget).invoke_async()awaits async listeners.
import asyncio
from pynetevents import EventSlot
def sync(msg): print(msg)
async def async_listener(msg): print(msg)
slot = EventSlot("event")
slot += sync
slot += async_listener
slot("Fire-and-forget")
asyncio.run(slot.invoke_async("Awaited"))
- Slots can be configured in the constructor:
EventSlot("name", propagate_exceptions=True, allow_duplicate_listeners=True)
Event Descriptor
Event is a descriptor to declare class-level events:
from pynetevents import Event
class MyApp:
on_update = Event(propagate_exceptions=True)
on_weak = Event(use_weakref_slot=True, allow_duplicate_listeners=True)
app = MyApp()
app.on_update += lambda msg: print(msg)
app.on_update("Update fired")
- Automatically creates per-instance slot.
+=/-=preferred; direct assignment is restricted.- Configuration must match if manually assigning a slot in
__init__as well.
Exceptions
EventExecutionError— Raised when a listener fails.DuplicateEventListenerError— Raised when adding a duplicate listener when not allowed.
Metadata
Release files for pynetevents 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pynetevents-1.0.0.tar.gz | 13.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pynetevents-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.0 kB
Release files / pynetevents-1.0.0.tar.gz
| Download URL | pynetevents-1.0.0.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6dc9ba77f94181878d242ff1129d51e42a5b8956b48ab3622a01a7b0cd8efc9e
|
|
BLAKE2b-256 checksum How to use checksums |
9a5abc4ff49d8f9decf639672f441683f91ab12f8b18abbdc8a21a87516f62a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|
Release files / pynetevents-1.0.0-py3-none-any.whl
| Download URL | pynetevents-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d8100010a7da956e38f5310627f072506a3a3fb50c022607b21f933ab2fc184a
|
|
BLAKE2b-256 checksum How to use checksums |
2c66868ef7507a2e66398b291211d0323dd53636e84bdb14e7921ef30cd96786
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|