This is my callback function and I set the rcv_buffer to be global
def rx_callback(ok, payload):
global n_rcvd, n_right, rcv_buffer
n_rcvd += 1
(pktno,) = struct.unpack('!H', payload[0:2])
if ok:
n_right += 1
rcv_buffer.append((pktno, payload))
And in the main() I continuously check the buffer to see whether the buffer is empty:
while 1:
while len(rcv_buffer) > 0:
(pktno, payload) = rcv_buffer.pop(0)
print 'pktno = ', pktno, 'payload = ', payload[2:]
But I didn’t do any synchronizations! Can I be sure that my operations on the list will not crash?Thanks!
In CPython, this is safe due to the interpreted nature of the execution, and due to the Global Interpeter Lock (which ensures that only a single bytecode from a single thread is executing at any given time).
Other Python implementations might be quite so forgiving though…