66from threading import Timer
77
88from .continuation import Continuation
9- from .continuation import SendContinuation
10- from .continuation import ThrowContinuation
119from .invocation import Invocation
1210from .invocation import LocalInvocationSuspended
1311from .qio import Qio
1412from .queue import Queue
1513from .queue import ShutDown
1614from .queuespec import QueueSpec
15+ from .result import Err
16+ from .result import Ok
1717from .thread import Thread
1818
1919
2020class Worker :
2121 def __init__ (self , qio : Qio , queuespec : QueueSpec ):
2222 self .__qio = qio
2323
24- self .__tasks = Queue [Invocation | SendContinuation | ThrowContinuation ]()
24+ self .__tasks = Queue [Invocation | Continuation ]()
2525 self .__consumer = self .__qio .consume (queuespec )
2626 self .__continuer_events = self .__qio .subscribe ({LocalInvocationSuspended })
2727
@@ -102,6 +102,7 @@ def listener():
102102 waiting [event .suspension .start ()] = Continuation (
103103 invocation = event .invocation ,
104104 generator = event .generator ,
105+ result = Ok (None ),
105106 )
106107 # Replace ``new`` before setting the result
107108 # to avoid short busy wait loops.
@@ -137,10 +138,10 @@ def listener():
137138 )
138139 with suppress (ShutDown ):
139140 self .__tasks .put (
140- ThrowContinuation (
141+ Continuation (
141142 invocation = continuation .invocation ,
142143 generator = continuation .generator ,
143- exception = exception ,
144+ result = Err ( exception ) ,
144145 )
145146 )
146147 else :
@@ -149,10 +150,10 @@ def listener():
149150 )
150151 with suppress (ShutDown ):
151152 self .__tasks .put (
152- SendContinuation (
153+ Continuation (
153154 invocation = continuation .invocation ,
154155 generator = continuation .generator ,
155- value = value ,
156+ result = Ok ( value ) ,
156157 ),
157158 )
158159
@@ -180,7 +181,7 @@ def __runner(self):
180181 case Invocation () as invocation :
181182 self .__consumer .start (invocation )
182183 self .__run_invocation (invocation )
183- case SendContinuation () | ThrowContinuation () as continuation :
184+ case Continuation () as continuation :
184185 self .__consumer .resume (continuation .invocation )
185186 self .__run_continuation (continuation )
186187
@@ -195,22 +196,18 @@ def __run_invocation(self, invocation: Invocation):
195196 if isinstance (result , Awaitable ):
196197 generator = result .__await__ ()
197198 self .__run_continuation (
198- SendContinuation (
199+ Continuation (
199200 invocation = invocation ,
200201 generator = generator ,
201- value = None ,
202+ result = Ok ( None ) ,
202203 )
203204 )
204205 else :
205206 self .__consumer .succeed (invocation , result )
206207
207- def __run_continuation (self , continuation : SendContinuation | ThrowContinuation ):
208+ def __run_continuation (self , continuation : Continuation ):
208209 """Process a continuation task."""
209- match continuation :
210- case SendContinuation ():
211- method = continuation .send
212- case ThrowContinuation ():
213- method = continuation .throw
210+ method = continuation .resume
214211
215212 try :
216213 suspension = method ()
0 commit comments