@@ -98,6 +98,34 @@ def on_bind_driver(self, driver):
98
98
self .input .add_preaction_cb (driver )
99
99
100
100
101
+ class KeepRunningInstrumentationThread (threading .Thread ):
102
+ """Thread class with a stop() method. The thread itself has to check
103
+ regularly for the stopped() condition."""
104
+
105
+ def __init__ (self , poco , port_to_ping ):
106
+ super (KeepRunningInstrumentationThread , self ).__init__ ()
107
+ self ._stop_event = threading .Event ()
108
+ self .poco = poco
109
+ self .port_to_ping = port_to_ping
110
+
111
+ def stop (self ):
112
+ self ._stop_event .set ()
113
+
114
+ def stopped (self ):
115
+ return self ._stop_event .is_set ()
116
+
117
+ def run (self ):
118
+ while not self .stopped ():
119
+ if getattr (self .poco , "_instrument_proc" , None ) is not None :
120
+ stdout , stderr = self .poco ._instrument_proc .communicate ()
121
+ print ('[pocoservice.apk] stdout: {}' .format (stdout ))
122
+ print ('[pocoservice.apk] stderr: {}' .format (stderr ))
123
+ if not self .stopped ():
124
+ print ('[pocoservice.apk] retrying instrumentation PocoService' )
125
+ self .poco ._start_instrument (self .port_to_ping ) # 尝试重启
126
+ time .sleep (1 )
127
+
128
+
101
129
class AndroidUiautomationPoco (Poco ):
102
130
"""
103
131
Poco Android implementation for testing **Android native apps**.
@@ -179,7 +207,8 @@ def __init__(self, device=None, using_proxy=True, force_restart=False, use_airte
179
207
raise RuntimeError ("unable to launch AndroidUiautomationPoco" )
180
208
if ready :
181
209
# 首次启动成功后,在后台线程里监控这个进程的状态,保持让它不退出
182
- self ._keep_running_instrumentation (p0 )
210
+ self ._keep_running_thread = KeepRunningInstrumentationThread (self , p0 )
211
+ self ._keep_running_thread .start ()
183
212
184
213
endpoint = "http://{}:{}" .format (self .device_ip , p1 )
185
214
agent = AndroidPocoAgent (endpoint , self .ime , use_airtest_input )
@@ -198,22 +227,6 @@ def _is_running(self, package_name):
198
227
return True
199
228
return False
200
229
201
- def _keep_running_instrumentation (self , port_to_ping ):
202
- print ('[pocoservice.apk] background daemon started.' )
203
-
204
- def loop ():
205
- while True :
206
- if self ._instrument_proc is not None :
207
- stdout , stderr = self ._instrument_proc .communicate ()
208
- print ('[pocoservice.apk] stdout: {}' .format (stdout ))
209
- print ('[pocoservice.apk] stderr: {}' .format (stderr ))
210
- print ('[pocoservice.apk] retrying instrumentation PocoService' )
211
- self ._start_instrument (port_to_ping ) # 尝试重启
212
- time .sleep (1 )
213
- t = threading .Thread (target = loop )
214
- t .daemon = True
215
- t .start ()
216
-
217
230
def _start_instrument (self , port_to_ping , force_restart = False ):
218
231
if not force_restart :
219
232
try :
@@ -281,6 +294,12 @@ def on_pre_action(self, action, ui, args):
281
294
msg = msg .decode ('utf-8' )
282
295
snapshot (msg = msg )
283
296
297
+ def stop_running (self ):
298
+ print ('[pocoservice.apk] stopping PocoService' )
299
+ self ._keep_running_thread .stop ()
300
+ self ._keep_running_thread .join (3 )
301
+ self .adb_client .shell (['am' , 'force-stop' , PocoServicePackage ])
302
+
284
303
285
304
class AndroidUiautomationHelper (object ):
286
305
_nuis = {}
0 commit comments