@@ -77,7 +77,8 @@ def process_call(self, context, bufchan, event, functor):
77
77
context_gen = context .middleware_call_procedure (functor , * event .args )
78
78
methods = context_gen .next ()
79
79
wchannel = WrappedEvents (bufchan )
80
- server = core .ServerBase (wchannel , methods , heartbeat = None )
80
+ server = core .ServerBase (wchannel , methods , heartbeat = None ,
81
+ allow_remote_stop = True )
81
82
bufchan .emit ('CTX' , (None ,))
82
83
try :
83
84
server .run ()
@@ -105,13 +106,32 @@ def process_answer(self, context, bufchan, event, method,
105
106
wchannel = WrappedEvents (bufchan )
106
107
107
108
class ContextClient (core .ClientBase ):
109
+ def __init__ (self , channel ):
110
+ self ._closed = False
111
+ super (ContextClient , self ).__init__ (channel , heartbeat = None )
112
+
108
113
def close (self ):
109
- self ._channel .emit ('CTX_CLOSE' , (None ,))
114
+ if self ._closed :
115
+ return
116
+ self ('_zerorpc_stop' )
110
117
super (ContextClient , self ).close ()
118
+ wchannel .close ()
111
119
bufchan .close ()
112
120
bufchan .channel .close ()
113
121
bufchan .channel .channel .close ()
122
+ self ._closed = True
123
+
124
+ def __call__ (self , method , * args , ** kargs ):
125
+ if self ._closed :
126
+ raise ValueError ('I/O operation on closed context' )
127
+ return super (ContextClient , self ).__call__ (method , * args , ** kargs )
128
+
129
+ def __enter__ (self ):
130
+ return self
131
+
132
+ def __exit__ (self , * args ):
133
+ self .close ()
114
134
115
- return ContextClient (wchannel , heartbeat = None )
135
+ return ContextClient (wchannel )
116
136
117
137
patterns_list = [ReqContext (), ReqStream (), ReqRep ()]
0 commit comments