3
3
import sys
4
4
import threading
5
5
import time
6
- from .common import Configurable , RuntimeContext , Queue
7
- from .frontend import FrontendThread
8
- from .transport import TransportThread
9
- from .backend import BackendThread
10
- from .othread import MultithreadManager
11
- from .utils import getatters
12
6
7
+ from .backend import BackendThread , BackendDriver
8
+ from .common import Configurable , Queue , RuntimeContext
9
+ from .frontend import FrontendThread , FrontendDriver
10
+ from .transport import TransportThread , TransportDriver , BridgeDriver
11
+ from .utils import getatters , getlastattr
12
+ from .othread import OthreadManager
13
13
14
- class MultithreadEngine (Configurable ):
15
14
16
- def __init__ (self , config ):
17
- super (MultithreadEngine , self ).__init__ (config )
18
- self .__ensure ()
15
+ class Engine (Configurable ):
16
+
17
+ def __init__ (self , config , runtime_context ):
18
+ super (Engine , self ).__init__ (config )
19
19
self .__start_lock = threading .Lock ()
20
20
self .__started = False
21
21
self .__stopped = False
22
- self ._runtime_context = RuntimeContext ()
23
-
24
- def __ensure (self ):
25
- engine_config = self .config .engine
26
- assert any ([hasattr (engine_config , x )
27
- for x in ('transport' , 'backend' )])
28
-
29
- def __setup (self ):
30
- runtime_context = self ._runtime_context
31
- engine_config = self .config .engine
32
-
33
- runtime_context .frontend_thread_queue = Queue .Queue (
34
- engine_config .frontend .queue_size )
35
-
36
- runtime_context .frontend_thread = MultithreadManager (
37
- self .config , runtime_context ,
38
- FrontendThread , engine_config .frontend .thread_num ,
39
- engine_config .frontend .driver_cls )
40
- runtime_context .frontend_thread .setDaemon (True )
41
-
42
- if hasattr (engine_config , 'transport' ):
43
-
44
- if hasattr (engine_config , 'backend' ):
45
- runtime_context .backend_thread_queue = Queue .Queue (
46
- engine_config .backend .queue_size )
47
-
48
- runtime_context .transport_thread = MultithreadManager (
49
- self .config , runtime_context ,
50
- TransportThread , engine_config .transport .thread_num ,
51
- engine_config .transport .driver_cls )
22
+ self ._runtime_context = runtime_context
52
23
53
- if hasattr (engine_config , 'backend' ):
54
- runtime_context .backend_thread = MultithreadManager (
55
- self .config , runtime_context ,
56
- BackendThread , engine_config .backend .thread_num ,
57
- engine_config .backend .driver_cls )
58
-
59
- def __start (self ):
24
+ def start (self ):
60
25
26
+ self .__acquire_start_lock (False , 'Can not start twice' )
61
27
runtime_context = self ._runtime_context
62
28
m = getatters (runtime_context , [
63
29
'backend_thread' ,
@@ -87,11 +53,6 @@ def __acquire_start_lock(self, started, err_msg):
87
53
finally :
88
54
self .__start_lock .release ()
89
55
90
- def start (self ):
91
- self .__acquire_start_lock (False , 'Can not start twice' )
92
- self .__setup ()
93
- self .__start ()
94
-
95
56
def __stop (self ):
96
57
runtime_context = self ._runtime_context
97
58
runtime_context .frontend_thread .stop ()
@@ -106,3 +67,58 @@ def stop(self):
106
67
if not self .__stopped :
107
68
self .__stop ()
108
69
self .__start_lock .release ()
70
+
71
+
72
+ class DEFAULT_FRONTEND_CONFIG (object ):
73
+ thread_num = 1
74
+ driver_cls = FrontendDriver
75
+
76
+
77
+ class DEFAULT_TRANSPORT_CONFIG (object ):
78
+ thread_num = 10
79
+
80
+
81
+ class DEFAULT_BACKEND_CONFIG (object ):
82
+ thread_num =
83
+
84
+
85
+ def create (frontend_config = DEFAULT_FRONTEND_CONFIG ,
86
+ transport_config = DEFAULT_TRANSPORT_CONFIG ,
87
+ backend_config = DEFAULT_BACKEND_CONFIG ,
88
+ app_config = None , runtime_context = None ):
89
+
90
+ if frontend_config is None :
91
+ raise ValueError ('No frontend config' )
92
+
93
+ if transport_config is None and backend_config is None :
94
+ raise ValueError ('Config at least one of transport/backend' )
95
+
96
+ runtime_context = runtime_context if runtime_context is not None else RuntimeContext ()
97
+
98
+ runtime_context .frontend_thread_queue = Queue .Queue (
99
+ engine_config .frontend .queue_size )
100
+
101
+ runtime_context .frontend_thread = OthreadManager (
102
+ app_config , runtime_context ,
103
+ FrontendThread , engine_config .frontend .thread_num ,
104
+ engine_config .frontend .driver_cls )
105
+ runtime_context .frontend_thread .setDaemon (True )
106
+
107
+ if hasattr (engine_config , 'transport' ):
108
+
109
+ if hasattr (engine_config , 'backend' ):
110
+ runtime_context .backend_thread_queue = Queue .Queue (
111
+ engine_config .backend .queue_size )
112
+
113
+ runtime_context .transport_thread = OthreadManager (
114
+ app_config , runtime_context ,
115
+ TransportThread , engine_config .transport .thread_num ,
116
+ engine_config .transport .driver_cls )
117
+
118
+ if hasattr (engine_config , 'backend' ):
119
+ runtime_context .backend_thread = OthreadManager (
120
+ app_config , runtime_context ,
121
+ BackendThread , engine_config .backend .thread_num ,
122
+ engine_config .backend .driver_cls )
123
+
124
+ return Engine (engine_config , runtime_context )
0 commit comments