我编写了一个Python程序,用于在UDP上发送和接收大型文件。现在,当我在10千兆位以太网电缆上在两台计算机之间传输时,我可以达到大约.01GB/s的速度。我想大大提高速度,但我不确定最好的方法是什么。
无论它的价值是什么,我必须使用UDP进行传输。我编写的程序只是一个大型项目的测试,而为该项目发送数据的设备不能利用TCP流。此外,我主要关注的是快速接收数据报的最佳方法,或者至少是确保接收端不出现任何瓶颈的最佳方法。
现在,我的程序通过将一个大文件块成几个块来工作,这些文件将成为要发送的数据报。这些数据报被发送,然后接收方做一些事情来确保它得到正确的数据并相应地对其进行排序。
发送代码(简化为基本代码)
buf = 32000 #Size of each chunk/packet
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
host ="127.0.0.1"
port = 5005
addr = (host,port)
def read_in_chunks(infile, chunk_size=buf):
"""Chunk the file before we send it.
Arguments:
infile -- the file to chunk
chunk_size -- the size of the chunk in bytes (default 32KB)
"""
while True:
chunk = infile.read(chunk_size)
if chunk:
yield chunk
else:
# The chunk was empty, which means we're at the end of the file
return
def run():
for chunk in read_in_chunks(f):
if(s.sendto(chunk,addr) and s.sendto(id,addr)):
#Some acknowledgment stuff - removed for clarity (also noted to not impact performance)
local_ID += 1接收代码:
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
buf = 32000 # Buffer size in bytes for each chunk
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
try:
while(dataChunk):
actualNumChunks += 1
f.write(dataChunk)
sock.settimeout(2)
dataChunk, addr = sock.recvfrom(buf)
packID, junkAddr = sock.recvfrom(buf)
packIDString = str(packID)
except socket.timeout:
f.close()
sock.close()
print "File received!" # --KEEP
print "A total of " + str(actualNumChunks) +" chunks were received" --KEEP我不确定优化我的代码是问题所在(还没有测试),还是还有其他问题(更好?)提高文件传输速度的方法。如果这里的细节很少,我很抱歉,但是如果你需要更多的信息,请告诉我。
谢谢!
发布于 2016-06-23 14:58:26
一些尝试的方法:
其他要点:
发布于 2022-01-14 17:43:29
对于您的接收器来说,您需要编写文件,这可能是一个瓶颈。需要尝试的一些事情是将所有的"dataChunk“写到队列中。然后有一个单独的线程或进程(多处理)从这个队列中读取,该队列对文件进行写入。这将从while循环卸载文件写IO,并可能允许您处理更多的数据包。
https://stackoverflow.com/questions/37995053
复制相似问题