'Thread-safe buffer with list
I have the following small class here:
class MyBuffer():
def __init__(self):
self.buffer = []
self.thread = threading.Thread(target=self.process)
self.thread.start()
def pop_all(self):
result, self.buffer = self.buffer, []
return result
def process(self):
while True:
current_buffer = self.pop_all()
# Do something with current_buffer
time.sleep(60)
def append_buffer(self, param):
self.buffer.append(param)
The append_buffer will be called at some part of the application to add items to the list. While the process method runs every 60 seconds to get all the items, clear the buffer and process those items that were buffered.
The question if the pop_all method is thread safe here? If not what would be a better approach?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
