1414
1515import atexit
1616import json
17+ import threading
1718
1819import etcd3gw
1920from etcd3gw import exceptions as etcd3gw_exc
2021from etcd3gw .utils import _decode
2122from etcd3gw .utils import _encode
2223from etcd3gw .utils import _increment_last_byte
23- import eventlet
2424from oslo_log import log as logging
25+ from oslo_service import threadgroup
2526from oslo_utils import netutils
2627from oslo_utils import uuidutils
2728import tenacity
3233
3334LOG = logging .getLogger (__name__ )
3435
35- THREAD_POOL = eventlet . greenpool . GreenPool ()
36+ THREAD_POOL = threadgroup . ThreadGroup ()
3637
3738
3839class ShutdownTimeout (Exception ):
@@ -48,12 +49,12 @@ def _wait_for_threads():
4849 and performing switch configuration operations which should not be
4950 interrupted.
5051 """
52+ active_threads = len (THREAD_POOL .threads )
5153 LOG .info ("Waiting %d seconds for %d threads to complete" ,
52- SHUTDOWN_TIMEOUT , THREAD_POOL . running () )
54+ SHUTDOWN_TIMEOUT , active_threads )
5355 try :
54- with eventlet .Timeout (SHUTDOWN_TIMEOUT , ShutdownTimeout ):
55- THREAD_POOL .waitall ()
56- except ShutdownTimeout :
56+ THREAD_POOL .stop (graceful = True , timeout = SHUTDOWN_TIMEOUT )
57+ except Exception :
5758 LOG .error ("Timed out waiting for threads to complete" )
5859 else :
5960 LOG .info ("Finished waiting for threads to complete" )
@@ -365,13 +366,12 @@ def do_work():
365366
366367 @staticmethod
367368 def _spawn (work_fn ):
368- # TODO(johngarbutt) remove hard eventlet dependency
369- # in a similar way to etcd3gw
370369 # Sleep to let possible other work to batch together
371- eventlet .sleep (0 )
370+ # This works with both eventlet and native threading
371+ threading .Event ().wait (0.001 )
372372 # Run all pending tasks, which might be a no op
373373 # if pending tasks already ran
374- THREAD_POOL .spawn_n (work_fn )
374+ THREAD_POOL .add_thread (work_fn )
375375
376376 def _execute_pending_batches (self , device , item ):
377377 """Execute all batches currently registered.
0 commit comments