Python saving by reference to avoid locks

asked 2014-01-14 12:25:17 -0600

Daniel Stonier gravatar image

updated 2014-01-28 17:19:02 -0600

ngrennan gravatar image

While digging in rospy to understand better the multi-threaded nature of rospy's callbacks, I came across this code from Ken:

def has_connection(self, endpoint_id):
    # save reference to avoid lock
    conn = self.connections
    for c in conn:
        if c.endpoint_id == endpoint_id:
            return True
    return False

There are quite a few other instances of the same comment/method in topics.py. It's a riddle to me though and google isn't providing much insight for me on the topic (pardon the pun) either.

Can anyone shed some light on why saving the reference helps avoid the lock?

edit retag flag offensive close merge delete