Переглянути джерело

buttons增加mutex_mode选项

wangweimin 5 роки тому
батько
коміт
2089d4671c
4 змінених файлів з 21 додано та 7 видалено
  1. 1 1
      wsrepl/framework.py
  2. 7 0
      wsrepl/input_ctrl.py
  3. 1 2
      wsrepl/interact.py
  4. 12 4
      wsrepl/output.py

+ 1 - 1
wsrepl/framework.py

@@ -68,7 +68,7 @@ class Task:
 
             self.task_finished = True
 
-            gen_log.debug('Task[%s] finished, self.coros:%s', self.coro_id, self.ws.coros)
+            gen_log.debug('Task[%s] finished', self.coro_id)
 
     def _tornado_future_callback(self, future):
         del self.pending_futures[id(future)]

+ 7 - 0
wsrepl/input_ctrl.py

@@ -2,10 +2,15 @@ import json
 import logging
 
 from .framework import WebIOFuture, Global
+from tornado.log import gen_log
 
 logger = logging.getLogger(__name__)
 
 
+def run_async(coro):
+    Global.active_ws.inactive_coro_instances.append(coro)
+
+
 def send_msg(cmd, spec=None):
     msg = dict(command=cmd, spec=spec, coro_id=Global.active_coro_id)
     Global.active_ws.write_message(json.dumps(msg))
@@ -107,5 +112,7 @@ async def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_func
 
             if all_valid:
                 break
+        else:
+            gen_log.warning("Unhandled Event: %s", event)
 
     return event['data']

+ 1 - 2
wsrepl/interact.py

@@ -8,8 +8,7 @@ from .input_ctrl import send_msg, single_input, input_control
 logger = logging.getLogger(__name__)
 
 
-def run_async(coro):
-    Global.active_ws.inactive_coro_instances.append(coro)
+
 
 
 TEXT = 'text'

+ 12 - 4
wsrepl/output.py

@@ -3,7 +3,7 @@ import logging
 from collections.abc import Mapping
 
 from .framework import Global, Task
-from .input_ctrl import send_msg, single_input, input_control, next_event
+from .input_ctrl import send_msg, single_input, input_control, next_event, run_async
 import asyncio
 import inspect
 
@@ -50,7 +50,7 @@ def put_table(tdata):
     text_print('\n'.join(res))
 
 
-def buttons(buttons, onclick_coro, save=None):
+def buttons(buttons, onclick_coro, save=None, mutex_mode=False):
     """
     :param buttons: button列表, button可用形式:
         {value:, label:, }
@@ -58,6 +58,7 @@ def buttons(buttons, onclick_coro, save=None):
         value 单值,label等于value
     :param onclick_coro: CallBack(data, save) todo 允许onclick_coro非coro
     :param save:
+    :param mutex_mode: 互斥模式,回调在运行过程中,无法响应同一回调
     :return:
     """
 
@@ -76,13 +77,20 @@ def buttons(buttons, onclick_coro, save=None):
         while True:
             event = await next_event()
             assert event['event'] == 'callback'
+            coro = None
             if asyncio.iscoroutinefunction(onclick_coro):
-                await onclick_coro(event['data'], save)
+                coro = onclick_coro(event['data'], save)
             elif inspect.isgeneratorfunction(onclick_coro):
-                await asyncio.coroutine(onclick_coro)(save, event['data'])
+                coro = asyncio.coroutine(onclick_coro)(save, event['data'])
             else:
                 onclick_coro(event['data'], save)
 
+            if coro is not None:
+                if mutex_mode:
+                    await coro
+                else:
+                    run_async(coro)
+
     print('Global.active_ws', Global.active_ws)
     callback = Task(callback_coro(), Global.active_ws)
     callback.coro.send(None)  # 激活,Non't callback.step() ,导致嵌套调用step  todo 与inactive_coro_instances整合