ソースを参照

synchonous terminals

Bastien Sevajol 7 年 前
コミット
8c5cfeef14
共有2 個のファイルを変更した27 個の追加10 個の削除を含む
  1. 5 3
      sandbox/engulf/run.py
  2. 22 7
      synergine2/terminals.py

+ 5 - 3
sandbox/engulf/run.py ファイルの表示

@@ -42,8 +42,8 @@ class GameTerminal(Terminal):
42 42
         GrassSpawn,
43 43
     ]
44 44
 
45
-    def __init__(self):
46
-        super().__init__()
45
+    def __init__(self, *args, **kwargs):
46
+        super().__init__(*args, **kwargs)
47 47
         self.gui = None
48 48
 
49 49
     def receive(self, package: TerminalPackage):
@@ -137,7 +137,9 @@ def main():
137 137
     core = Core(
138 138
         simulation=simulation,
139 139
         cycle_manager=CycleManager(simulation=simulation),
140
-        terminal_manager=TerminalManager([GameTerminal()]),
140
+        terminal_manager=TerminalManager([GameTerminal(
141
+            asynchronous=False,
142
+        )]),
141 143
     )
142 144
     core.run()
143 145
 

+ 22 - 7
synergine2/terminals.py ファイルの表示

@@ -42,13 +42,14 @@ class Terminal(object):
42 42
     # who are not instance of listed classes
43 43
     subscribed_events = [Event]
44 44
 
45
-    def __init__(self):
45
+    def __init__(self, asynchronous: bool=True):
46 46
         self._input_queue = None
47 47
         self._output_queue = None
48 48
         self._stop_required = False
49 49
         self.subjects = {}
50 50
         self.cycle_events = []
51 51
         self.event_handlers = collections.defaultdict(list)
52
+        self.asynchronous = asynchronous
52 53
 
53 54
     def accept_event(self, event: Event) -> bool:
54 55
         for event_class in self.subscribed_events:
@@ -85,6 +86,8 @@ class Terminal(object):
85 86
 
86 87
     def receive(self, package: TerminalPackage):
87 88
         self.update_with_package(package)
89
+        # End of cycle management signal
90
+        self.send(TerminalPackage(is_cycle=True))
88 91
 
89 92
     def send(self, package: TerminalPackage):
90 93
         self._output_queue.put(package)
@@ -153,11 +156,23 @@ class TerminalManager(object):
153 156
 
154 157
     def receive(self) -> [TerminalPackage]:
155 158
         packages = []
156
-        for input_queue in self.inputs_queues.values():
157
-            try:
158
-                while True:
159
-                    packages.append(input_queue.get(block=False, timeout=None))
160
-            except Empty:
161
-                pass  # Queue is empty
159
+        for terminal, input_queue in self.inputs_queues.items():
160
+            # When terminal is synchronous, wait it's cycle package
161
+            if not terminal.asynchronous:
162
+                continue_ = True
163
+                while continue_:
164
+                    package = input_queue.get()
165
+                    # In case where terminal send package before end of cycle
166
+                    # management
167
+                    continue_ = not package.is_cycle
168
+                    packages.append(package)
169
+            else:
170
+                try:
171
+                    while True:
172
+                        packages.append(
173
+                            input_queue.get(block=False, timeout=None),
174
+                        )
175
+                except Empty:
176
+                    pass  # Queue is empty
162 177
 
163 178
         return packages