thread safety - Is callFromThread threadsafe -
i looked @ code callfromthread. it's appending callables threadcallqueue list. if multiple threads call callfromthread, how can callfromthread thread-safe? in other words, without lock on threadcallqueue, how can callfromthread threadsafe? missing basic?
multiple threads can add items list (these producers) , it's done through append() call (so, @ end of list):
self.threadcallqueue.append((f, args, kw))
only main thread ever reads items , removes them list (this 1 consumer) , reads @ beginning of list:
# keep track of how many calls make, we're # making them, in case call added queue # while we're in loop. count = 0 total = len(self.threadcallqueue) (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 1 thread reads beginning of list , others write @ end, thread-safe as long python lists are. noted in function's source code:
# lists thread-safe in cpython, not in jython # bug in jython, until fixed code # won't work in jython.
Comments
Post a Comment