do I need lock to protect mutli-thread race condition in my code
<p>Using Python 2.7 on Windows, and will use Jython which support true multi-threading. The method <code>sendMessage</code> is used to receive message from a specific client, and the client may send the same message to a few other clients (which is what parameter <code>receivers</code> is for, and <code>receivers</code> is a list). The method <code>receiveMessage</code> is used to receive message for a specific client, which are sent from other clients.</p>

<p>The question is whether I need any locks for method <code>sendMessage</code> and <code>receiveMessage</code>? I think there is no need, since even if a client X is receiving its message, it is perfect fine for another client Y to append to message pool to deliver message to client X. And I think for defaultdict/list, append/pop are both atomic and no need for protection.</p>

<p>Please feel free to correct me if I am wrong.</p>

<pre><code>from collections import defaultdict

class Foo:
    def __init__(self):
        # key: receiver client ID, value: message
        self.messagePool = defaultdict(list)
    def sendMessage(self, receivers, message):
        # check valid for receivers
        for r in receivers:
            self.messagePool[r].append(message)
    def receiveMessage(self, clientID):
        result = []
        while len(self.messagePool[clientID]) &gt; 0:
            result.append(self.messagePool[clientID].pop(0))

        return result
</code></pre>

<p>I think this question is already well-answered for CPython <a href="http://stackoverflow.com/questions/17682484/is-collections-defaultdict-thread-safe">here</a> and <a href="http://stackoverflow.com/questions/6319207/are-lists-thread-safe">here</a> (basically, you're safe because of GIL, although nothing in documentation (like on <a href="https://docs.python.org/2/library/collections.html#collections.defaultdict" rel="nofollow">defaultdict</a> or <a href="https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange" rel="nofollow">list</a>) officially says about that). But I understand your concern about Jython, so let's solve it using some official source, like <a href="https://hg.python.org/jython/" rel="nofollow">Jython source code</a>. A pythonic <code>list</code> is a <a href="https://hg.python.org/jython/file/tip/src/org/python/core/PyList.java" rel="nofollow">javaish <code>PyList</code></a> there with this kind of code:</p>

<pre><code>public void append(PyObject o) {
    list_append(o);
}
final synchronized void list_append(PyObject o) {
...
}
public PyObject pop(int n) {
    return list_pop(n);
}
final synchronized PyObject list_pop(int n) {
...
}
</code></pre>

<p>And as we have these methods <a href="http://stackoverflow.com/questions/1085709/what-does-synchronized-mean">synchronized</a>, we can be sure that list appends and pops are also thread-safe with Jython. Thus, your code seems to be safe wrt threading.</p>

<p>Although <code>Queue</code> suggestion is still valid one, it really is more appropriate for this use case.</p>

python multithreading python-2.7 jython