This is a little complicated…
Basically, I have this function:
def do_loop(self):
for line in self.connections[0].iter_lines():
print line
And, there is a thread constantly running alongside it, which will at arbitrary times change the value of connections[0].
If this happens, if connections[0] is externally changed by the thread, the loop will keep using the old connections[0], I need this to not happen, I need it to immediately use the new connections[0] instead.
For some background, for line in self.connections[0].iter_lines(): is reading data from the Twitter Streaming API using python-requests, hence .iter_lines().
Any ideas? Thanks in advance.
The
forloop is going to make an iterator, once, and it won’t keep checkingself.connections[0]in the loop. So as @mklauber said, use something likethreading.Event.Assuming that we have a
threading.Eventinstance asself.new_connand it gets set whenever there is a new connection:If you just need the loop to instantly terminate, you could handle it by making your
.iter_lines()method a generator, and making the generator do the check:This nicely encapsulates the loop. Your original code would work as shown, and it would stop when there was a new connection.