This is kind of a weird question so I'll explain:
I have a generator like this that is acting as a generator frontend to an IRC server:
def irc_iter(): # not the real code, simplified
msgs = get_msgs()
for msg in msgs:
if is_ping(msg):
pong()
else:
to_send = yield msg
for s in to_send:
send(s)
This, theoretically, should allow me to do something cool, like:
server = connect()
for line in server:
if should_respond(line):
server.send('WOW SUCH MESSAGE')
However, there's a hitch: generator.send
yields the next value as well. This means that server.send
is also giving me the next message... which I would prefer to handle like all the other messages, yielded as line
.
I know I can fix this in an ugly way, by just yielding a junk value after receiving a send, but I'm trying to keep my code elegant and that is the opposite. Is there a way to just tell the generator I don't want a new value yet?
Thanks.
yield
statements in my coroutines when they are this "composite" style of half-iterator, half-coroutine. I've had some success in rewriting such consumers to explicitlysend
all of the time, even if I'm sendingNone
the majority of the time. Not sure if that helps you here, but it's an idea. – Evergladessend()
andnext()
,send()
is justnext()
with an argument. – Geostatic