import functools


def spawning_callback(nursery, async_callback=None):
    """Return a new function that spawns *async_callback*.

    This can be also used as a decorator::

        @spawning_callback(nursery)
        def on_click():
            print("one")
            await trio.sleep(1)
            print("two")

        button['command'] = on_click

    Note that :class:`~AsyncButton` does this to its commands
    automatically, so usually you don't need to use this with buttons.
    """
    if async_callback is None:
        return functools.partial(spawning_callback, nursery)
    return functools.partial(nursery.spawn, async_callback)