Skip to content

Instantly share code, notes, and snippets.

@nanounanue
Forked from flashingpumpkin/coroutine.py
Created April 25, 2018 06:07
Show Gist options
  • Save nanounanue/b5bfe5f33b0afa92f0caacf965dc1c9f to your computer and use it in GitHub Desktop.
Save nanounanue/b5bfe5f33b0afa92f0caacf965dc1c9f to your computer and use it in GitHub Desktop.
Decorator for coroutine usage.
def coroutine(func):
"""
A decorator that turns a function with normal input into a coroutine.
This decorator takes care of priming the coroutine, doing the looping and
handling the closing.
The first argument of any decorated function is what was received when
data was sent to the coroutine via the ``.send`` method. All the other
``*args`` and ``**kwargs`` are what was passed to the decorated function
when instantiating the coroutine.
Using named arguments is recommended for less confusion.
::
@coroutine
def tailgrep(input = None, pattern = None):
if pattern in input:
print input
t = tailgrep(pattern = 'Python')
for line in lines:
t.send(line)
"""
def routine(*args, **kwargs):
try:
while True:
input = (yield)
func(input = input, *args, **kwargs)
except StopIteration:
pass
def start(*args, **kwargs):
coroutine = routine(*args, **kwargs)
coroutine.next()
return coroutine
return start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment