def sync_engine(func):
    """Queued version of the ``tornado.gen.engine``.

    Prevents calling of the wrapped function if there is already one instance of
    the function running asynchronously. Function will be called synchronously
    without blocking io_loop.

    This decorator can only be used on class methods, as it requires ``self``
    to make sure that calls are scheduled on instance level (connection) instead
    of class level (method).
    """
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        # Run method
        def run(args, kwargs):
            gen = func(self, *args, **kwargs)
            if isinstance(gen, types.GeneratorType):
                data.runner = SyncRunner(gen, finished)
                data.runner.run()
            else:
                return gen

        # Completion callback
        def finished():
            data.runner = None

            try:
                args, kwargs = data.queue.popleft()
                run(args, kwargs)
            except __HOLE__:
                pass

        # Get call queue for this instance and wrapped method
        queue = getattr(self, '_call_queue', None)
        if queue is None:
            queue = self._call_queue = dict()

        data = queue.get(func, None)
        if data is None:
            queue[func] = data = CallQueue()

        # If there's something running, queue call
        if data.runner is not None:
            data.queue.append((args, kwargs))
        else:
            # Otherwise run it
            run(args, kwargs)

    return wrapper
dataset/ETHPy150Open mrjoes/tornadio2/tornadio2/gen.py/sync_engine