OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2011 Google Inc. All Rights Reserved. | 2 # Copyright 2011 Google Inc. All Rights Reserved. |
3 # | 3 # |
4 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
5 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
6 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
7 # | 7 # |
8 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
9 # | 9 # |
10 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
(...skipping 16 matching lines...) Expand all Loading... | |
27 server's initializer is called successfully, the __exit__() function | 27 server's initializer is called successfully, the __exit__() function |
28 is guaranteed to be called when ServerManager.Run() completes. | 28 is guaranteed to be called when ServerManager.Run() completes. |
29 """ | 29 """ |
30 | 30 |
31 def __init__(self, is_record_mode): | 31 def __init__(self, is_record_mode): |
32 """Initialize a server manager.""" | 32 """Initialize a server manager.""" |
33 self.initializers = [] | 33 self.initializers = [] |
34 self.record_callbacks = [] | 34 self.record_callbacks = [] |
35 self.replay_callbacks = [] | 35 self.replay_callbacks = [] |
36 self.is_record_mode = is_record_mode | 36 self.is_record_mode = is_record_mode |
37 self.should_exit = False | |
37 | 38 |
38 def Append(self, initializer, *init_args, **init_kwargs): | 39 def Append(self, initializer, *init_args, **init_kwargs): |
39 """Append a server to the end of the list to run. | 40 """Append a server to the end of the list to run. |
40 | 41 |
41 Servers start in the order they are appended and stop in the | 42 Servers start in the order they are appended and stop in the |
42 opposite order. | 43 opposite order. |
43 | 44 |
44 Args: | 45 Args: |
45 initializer: a function that returns a server instance. | 46 initializer: a function that returns a server instance. |
46 A server needs to implement the with-statement interface. | 47 A server needs to implement the with-statement interface. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 server_exits = [] | 93 server_exits = [] |
93 exception_info = (None, None, None) | 94 exception_info = (None, None, None) |
94 try: | 95 try: |
95 for initializer, init_args, init_kwargs in self.initializers: | 96 for initializer, init_args, init_kwargs in self.initializers: |
96 server = initializer(*init_args, **init_kwargs) | 97 server = initializer(*init_args, **init_kwargs) |
97 if server: | 98 if server: |
98 server_exits.insert(0, server.__exit__) | 99 server_exits.insert(0, server.__exit__) |
99 server.__enter__() | 100 server.__enter__() |
100 while True: | 101 while True: |
101 time.sleep(1) | 102 time.sleep(1) |
103 if self.should_exit: | |
chrisgao (Use stgao instead)
2013/08/27 16:34:18
I think we don't need a lock for |should_exit|. It
tonyg
2013/08/28 01:35:58
Agreed. Python's GIL takes care of this.
| |
104 break | |
102 except: | 105 except: |
103 exception_info = sys.exc_info() | 106 exception_info = sys.exc_info() |
104 finally: | 107 finally: |
105 for server_exit in server_exits: | 108 for server_exit in server_exits: |
106 try: | 109 try: |
107 if server_exit(*exception_info): | 110 if server_exit(*exception_info): |
108 exception_info = (None, None, None) | 111 exception_info = (None, None, None) |
109 except: | 112 except: |
110 exception_info = sys.exc_info() | 113 exception_info = sys.exc_info() |
111 if exception_info != (None, None, None): | 114 if exception_info != (None, None, None): |
112 raise exception_info[0], exception_info[1], exception_info[2] | 115 raise exception_info[0], exception_info[1], exception_info[2] |
OLD | NEW |