Robotics StackExchange | Archived questions

Python saving by reference to avoid locks

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?

Asked by Daniel Stonier on 2014-01-14 13:25:17 UTC

Comments

Answers