1- # examples/01_signal_basic .py
1+ # examples/signal_basic .py
22
33"""
4- Async Signal Example
4+ Basic Signal-Slot Example
55
6- This example demonstrates the basic usage of TSignal with async slots :
6+ This example demonstrates the fundamental usage of TSignal with a synchronous slot :
771. Creating a signal
8- 2. Connecting an async slot
9- 3. Emitting a signal to async handler
8+ 2. Connecting a regular method as a slot (without @t_slot)
9+ 3. Emitting a signal to trigger slot execution
10+
11+ Key Points:
12+ - Showcases the most basic form of signal-slot connection.
13+ - The slot is a normal instance method of a class, not decorated with @t_slot.
14+ - Emphasizes that even without @t_slot, a callable method can act as a slot.
15+ - Introduces the concept of signal emission and immediate slot execution.
1016"""
1117
1218import asyncio
19+ import time
1320from tsignal .core import t_with_signals , t_signal , t_slot
1421
1522
@@ -34,20 +41,19 @@ def increment(self):
3441
3542
3643@t_with_signals
37- class AsyncDisplay :
44+ class Display :
3845 """
39- A simple display class that receives count updates and processes them asynchronously .
46+ A simple display class that receives count updates and processes them.
4047 """
4148
4249 def __init__ (self ):
4350 self .last_value = None
4451
45- @t_slot
46- async def on_count_changed (self , value ):
47- """Async slot that receives count updates"""
52+ def on_count_changed (self , value ):
53+ """slot that receives count updates"""
4854 print (f"Display processing count: { value } " )
49- # Simulate some async processing
50- await asyncio .sleep (1 )
55+ # Simulate some heavy processing
56+ time .sleep (1 )
5157 self .last_value = value
5258 print (f"Display finished processing: { value } " )
5359
@@ -59,28 +65,24 @@ async def main():
5965
6066 # Create instances
6167 counter = Counter ()
62- display = AsyncDisplay ()
68+ display = Display ()
6369
64- # Connect signal to async slot
65- counter .count_changed .connect (display , display .on_count_changed )
70+ # Connect signal to slot
71+ counter .count_changed .connect (display .on_count_changed )
6672
67- print ("Starting async counter example..." )
73+ print ("Starting counter example..." )
6874 print ("Press Enter to increment counter, or 'q' to quit" )
6975 print ("(Notice the 1 second delay in processing)" )
7076
7177 while True :
72- # Get input asynchronously
73- line = await asyncio .get_event_loop ().run_in_executor (None , input , "> " )
78+ line = input ("> " )
7479
7580 if line .lower () == "q" :
7681 break
7782
7883 # Increment counter which will emit signal
7984 counter .increment ()
8085
81- # Give some time for async processing to complete
82- await asyncio .sleep (0.1 )
83-
8486
8587if __name__ == "__main__" :
8688 asyncio .run (main ())
0 commit comments