I'm trying to parallelise my job, but I'm new to multithreading, so feel confused about the concrete implementation.
I have a socket listener, that saves data to a buffer. When buffer reaches his capacity I need to save its data to database. On one thread I want to start socket listener, while on parallel task I want to check the buffer status.
BufferQueue is just an extension of a python list, with method that allow to check whether the list has reached the specified size.
SocketManager is streaming data provider of a STREAM_URL I'm listening to. It use callback function to handle messages
But as I use callbacks to retrieve data I'm not sure that using shared variable is a right and optimal decision for that
buffer = BufferQueue(buffer_size=10000)
def start_listening_to_sokcet(client):
s = SocketManager(client)
s.start_socket(cb_new)
s.start()
def cb_new(message):
print("New message")
global buffer
for m in message:
#save data to buffer
def is_buffer_ready(buffer):
global buffer
print("Buffer state")
if buffer.ready():
#save buffer data to db
I'm appreciate if you can help me with this case
Lock.BufferQueue,SocketManager, etc. types come from, or at least what they do, it's pretty hard to offer anything that's not really vague. But I'd be wary of any API that used anis_buffer_readyfunction that the caller had to check periodically (or, worse, in a spin-loop); usually you're going to want something you can block on instead.