开发者

Is callFromThread threadsafe

I looked at the code for callFromThread.开发者_如何学运维 It's appending all the callables into threadCallQueue list. If multiple threads call callFromThread, how can callFromThread be thread-safe? In other words, without a lock on threadCallQueue, how can callFromThread be threadsafe? Am I missing something basic?


Multiple threads can add items to the list (these are the producers) and it's always done through an append() call (so, at the end of the list):

self.threadCallQueue.append((f, args, kw))

Only the main thread ever reads items and removes them from the list (this one is the consumer) and it always reads at the beginning of the list:

# Keep track of how many calls we actually make, as we're
# making them, in case another call is added to the queue
# while we're in this loop.
count = 0
total = len(self.threadCallQueue)
for (f, a, kw) in self.threadCallQueue:
    try:
        f(*a, **kw)
    except:
        log.err()
    count += 1
    if count == total:
        break
del self.threadCallQueue[:count]

So, since one thread reads from the beginning of the list and others write at the end, this is thread-safe as long as Python lists are. This is noted in the function's source code:

# lists are thread-safe in CPython, but not in Jython
# this is probably a bug in Jython, but until fixed this code
# won't work in Jython.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜