views:

44

answers:

0

I am currently making a network layer built that creates binary messages and then sends them to the appropriate network interface (this could be Ethernet, WiFi, Bluetooth, etc). In working on the TCP module, I'm using Python's socket module and everything works great except for when I try to send a series of messages quickly (such as a large file that requires multiple packets).

Right now, the program will create a packet or series of packets when the user wants to send data and puts them in a Queue object. A background Thread is running that continually tries to get items from the Queue and send them to the remote server. It seems that sometimes this happens too fast and multiple items from the Queue get sent at the same time which makes the client side's recv get too much information at once. The client may or may not be able to handle this, so it's best if I throttle the packets so they only get sent one at a time.

Interestingly enough, even putting a print command to tell me how much data is being sent is enough to let all the packets go through properly. I could put time.sleep(0.001) to fix the problem, but I'm worried that this is device-dependent and not a reliable way to solve the problem. Is there any to get the packets to come out of the Queue and get sent individually? Order isn't a problem as they'll be reordered if necessary on arrival. Thanks.