python函数的传递方式有哪些_使用Python提高UDP传输速度的最佳方法是什么?

我编写了一个

Python程序,用于通过UDP发送和接收大文件.现在,当使用万兆以太网电缆在两台计算机之间进行传输时,我可以达到约.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

我不确定优化我的代码是否是问题(尚待测试),或者是否有另一种(更好的?)方式来提高文件传输的速度.如果这里的细节很少,我很抱歉,但如果您需要更多信息,请告诉我.

谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值