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?
I looked at the code for callFromThread. It’s appending all the callables into threadCallQueue
Share
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):
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:
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: