You have probably answered this for yourself already, but for the benefit of anyone else that comes across this question, I was looking for the same thing and found the following:
The simple answer, as stated in the comments is; there appears to be no such thing in the standard library.
Answers on the subject of coroutines and their application on this site point to an implementation of a @coroutine decorator by David Beazely, a code snippet from which is listed below for convenience.
def coroutine(func):
def start(*args,**kwargs):
cr = func(*args,**kwargs)
cr.next()
return cr
return start
In reply to @DocDriven's comment; the above is a direct copy of David Beazely's implementation, which will work with versions of Python prior to v3.2. The accepted answers to this question gives a fuller explanation and discussion, but from Python v3.2 .next() has been replaced by the built in free function next(). So when using version 3.2 of Python and later the call to cr.next() is replaced with the call to the free function next(cr).
Updating David Beazely's original implementation to work with Python versions 3.2 and later, the full code with example use becomes:
def coroutine(func):
def start(*args,**kwargs):
cr = func(*args,**kwargs)
next(cr)
return cr
return start
# Example use
if __name__ == '__main__':
@coroutine
def grep(pattern):
print("Looking for %s" % pattern)
while True:
line = (yield)
if pattern in line:
print(line)
g = grep("python")
# Notice how you don't need a next() call here
g.send("Yeah, but no, but yeah, but no")
g.send("A series of tubes for pythons")
g.send("python generators rock!")
This worked for me with Python v3.7.3