I met a strange problem recently, hope someone here can help me out. I'm using Python2.7 in Ubuntu12.04, both python and OS are 64-bits.
In my code, I need to keep appending incoming data stream to a byte array, I use self.data += incomingdata to implement this, where incomingdata is the data I received from hardware devices. Then I will unpack the byte array some time later to parse the received data. The appending and parsing operations are all protected with lock.
The problem here is, when I use "+=" to append the byte stream, the data seems to be corrupted at some points (not happen consistently). There is no memory usage error, no overflow, etc. I monitored the memory usage of the program, it looks good.
Then, when I change "+=" to cStringIO.write to implement the appending operation, no problem at all, though it seems to be slower than the "+=" operation.
Can anyone tell me what is the exactly difference between cStringIo.write and "+=" when they are used to operate on byte streams? Will the "+=" operation cause any potential problems?