我正在编写一个小的多协议图像流服务器(在 Python 中),所有协议都运行良好,除了让我的 CPU 使用率高达 150% 的多播协议!
这是多播代码:
delay = 1./self.flux.ips
imgid = 0
lastSent = 0
while self.connected:
#self.printLog("Getting ready to fragment {}".format(imgid))
fragments = fragmentImage(self.flux.imageFiles[imgid], self.fragmentSize)
#self.printLog("Fragmented {} ! ".format(imgid))
# Checking if the delay has passed, to respected the framerate
while (time.time() - lastSent) < delay:
pass
# Sending the fragments
for fragmentid in range(len(fragments)):
formatedFragment = formatFragment(fragments[fragmentid], fragmentid*self.fragmentSize, len(self.flux.imageFiles[imgid]), imgid)
self.sendto(formatedFragment, (self.groupAddress, self.groupPort))
lastSent = time.time()
imgid = (imgid + 1) % len(self.flux.images)
UDP 协议也将图像作为片段发送,我没有任何 CPU 使用问题。请注意,客户端也有一些延迟来获取这些图像。