def local_outgoing_hook(handler=None, coro=None):
    """add a callback to run every time a greenlet is switched away from

    :param handler:
        the callback function, must be a function taking 2 arguments:

        - an integer indicating whether it is being called as an incoming (1)
          hook or as an outgoing (2) hook (in this case it will always be 2).
        - the coroutine being switched from (in this case it is the one
          indicated by the ``coro`` argument to ``local_outgoing_hook``.

        Be aware that only a weak reference to this function will be held.
    :type handler: function
    :param coro:
        the coroutine for which to apply the trace hook (defaults to current)
    :type coro: greenlet
    """
    if handler is None:
        return lambda h: local_outgoing_hook(h, coro)

    if not hasattr(handler, "__call__"):
        raise TypeError("trace hooks must be callable")

    if coro is None:
        coro = compat.getcurrent()

    log.info("setting a coroutine local outgoing hook callback")

    state.local_from_hooks.setdefault(coro, []).append(
        weakref.ref(handler))

    return handler