编写快速UDP服务器的问题 [英] Problem with writing fast UDP server
问题描述
大家好,
我正在努力编写快速的UDP服务器。它必须每秒处理大约10000
UDP数据包。我开始用非阻塞来构建那个
套接字和线程。不幸的是,我的方法根本不起作用。
我写了一个简单的案例测试:客户端和服务器。客户端在0.137447118759秒内发送2200
包。 tcpdump接收了2189个数据包,
这一点都不错。
但服务器只能处理700 - 870个数据包,当时它不是$
阻止,只有670? 700收到阻塞套接字。
客户端和服务器在同一个本地网络中工作
和tcpdump显示接收到的数据包非常正确。
我包含了一些UDP服务器的代码。
class PacketReceive(threading.Thread):
def __init __(self ,tname,socket,queue):
self._tname = tname
self._socket = socket
self._queue = queue
threading.Thread .__ init __(self,name = self._tname)
def run(self):
print''启动线程:'',self.getName()
cnt = 1
cnt_msgs = 0
而True:
尝试:
data = self._socket.recv(512)
msg =数据
cnt_msgs + = 1
总计+ = 1
#self._queue.put(msg)
print''thread:%s,cnt_msgs:%d''%(self.getName( ),
cnt_msgs)
除了:
通过
我也在用Qu eue,但这对两者都没有帮助。
我知道我做错了什么吗?
我读到Python套接字模块导致了一些延迟用
TCP服务器。他们建议为非延迟设置套接字选项:
" sock.setsockopt(SOL_TCP,TCP_NODELAY,1)"。对于UDP类型的套接字我找不到任何类似的选项
。
我需要更改套接字选项才能使它成为
工作得更快?
为什么服务器无法处理所有收到的数据包?套接字层
是否有错误?顺便说一句。我在Ubuntu 8.10上使用Python 2.5。
干杯
K
< blockquote> Krzysztof Retel< Kr ************* @ googlemail.comwrites:
但服务器只处理700 - 870数据包,当它不是
阻塞时,只有670个? 700收到阻塞插座。
你在做什么其他线程?您是否尝试过相同的代码
没有任何线程?
11月20日,3:34 * pm,Hrvoje Niksic< hnik .. 。@ xemacs.orgwrote:
Krzysztof Retel< Krzysztof.Re ... @ googlemail.comwrites:
但服务器只能处理700 - 870个数据包,当它是非阻止的b $ b时,只有670个? 700收到阻塞插座。
你在做什么其他线程? *您是否尝试过相同的代码
而没有任何线程?
我只有这一个帖子,我可以运行几次。
我尝试没有线程,结果相同,并非所有的包裹都被处理了。
11月20日16:03,Krzysztof Retel< Krzysztof.Re .. 。@ googlemail.com>
写道:
大家好,
我在挣扎编写快速UDP服务器。它必须每秒处理大约10000
UDP数据包。我开始用非阻塞来构建那个
套接字和线程。不幸的是,我的方法根本不起作用。
我写了一个简单的案例测试:客户端和服务器。客户端在0.137447118759秒内发送2200
包。 tcpdump接收了2189个数据包,
这一点都不错。
但服务器只能处理700 - 870个数据包,当时它不是$
阻止,只有670? 700收到阻塞套接字。
客户端和服务器在同一个本地网络中工作
和tcpdump显示接收到的数据包非常正确。
我包含了一些UDP服务器的代码。
class PacketReceive(threading.Thread):
* * def __init__ (self,tname,socket,queue):
* * * * self._tname = tname
* * * * self._socket = socket
* * * * self._queue = queue
* * * * threading.Thread .__ init __(self,name = self._tname)
* * def run(self):
* * * * print''启动线程:'',self.getName()
* * * * cnt = 1
* * * * cnt_msgs = 0
* * * *正确:
* * * * * *尝试:
* * * * * * * * data = self._socket.recv(512)
* * * * * * * * msg = data
* * * * * * * * cnt_msgs + = 1
* * * * * * * *总计+ = 1
* * * * * * * *#self._queue。放(味精)
* * * * * * * * print *''thread:%s,cnt_msgs:%d''%(self.getName(),
cnt_msgs)
* * * * * *除外:
* * * * * * * *通过
我也在使用Queue,但是这对两者都没有帮助。
我知道我做错了什么?
我读到Python套接字模块导致了一些延迟
TCP服务器。他们建议为非延迟设置*套接字选项:
" sock.setsockopt(SOL_TCP,TCP_NODELAY,1)"。对于UDP类型的套接字我找不到任何类似的选项
。
我需要更改套接字选项才能使它成为
工作得更快?
为什么服务器无法处理所有收到的数据包?套接字层
是否有错误?顺便说一句。我在Ubuntu 8.10上使用Python 2.5。
干杯
K
愚蠢的问题:你尝试删除打印件(例如每100封邮件打印一次
)吗?
Ciao
----
FB
Hi guys,
I am struggling writing fast UDP server. It has to handle around 10000
UDP packets per second. I started building that with non blocking
socket and threads. Unfortunately my approach does not work at all.
I wrote a simple case test: client and server. The client sends 2200
packets within 0.137447118759 secs. The tcpdump received 2189 packets,
which is not bad at all.
But the server only handles 700 -- 870 packets, when it is non-
blocking, and only 670 ? 700 received with blocking sockets.
The client and the server are working within the same local network
and tcpdump shows pretty correct amount of packets received.
I included a bit of the code of the UDP server.
class PacketReceive(threading.Thread):
def __init__(self, tname, socket, queue):
self._tname = tname
self._socket = socket
self._queue = queue
threading.Thread.__init__(self, name=self._tname)
def run(self):
print ''Started thread: '', self.getName()
cnt = 1
cnt_msgs = 0
while True:
try:
data = self._socket.recv(512)
msg = data
cnt_msgs += 1
total += 1
# self._queue.put(msg)
print ''thread: %s, cnt_msgs: %d'' % (self.getName(),
cnt_msgs)
except:
pass
I was also using Queue, but this didn''t help neither.
Any idea what I am doing wrong?
I was reading that Python socket modules was causing some delays with
TCP server. They recomended to set up socket option for nondelays:
"sock.setsockopt(SOL_TCP, TCP_NODELAY, 1) ". I couldn''t find any
similar option for UDP type sockets.
Is there anything I have to change in socket options to make it
working faster?
Why the server can''t process all incomming packets? Is there a bug in
the socket layer? btw. I am using Python 2.5 on Ubuntu 8.10.
Cheers
K
Krzysztof Retel <Kr*************@googlemail.comwrites:
But the server only handles 700 -- 870 packets, when it is non-
blocking, and only 670 a?? 700 received with blocking sockets.What are your other threads doing? Have you tried the same code
without any threading?
On Nov 20, 3:34*pm, Hrvoje Niksic <hnik...@xemacs.orgwrote:Krzysztof Retel <Krzysztof.Re...@googlemail.comwrites:But the server only handles 700 -- 870 packets, when it is non-
blocking, and only 670 ? 700 received with blocking sockets.
What are your other threads doing? *Have you tried the same code
without any threading?I have only this one thread, which I can run couple of times.
I tried without a threading and was the same result, not all packets
were processed.
On 20 Nov, 16:03, Krzysztof Retel <Krzysztof.Re...@googlemail.com>
wrote:Hi guys,
I am struggling writing fast UDP server. It has to handle around 10000
UDP packets per second. I started building that with non blocking
socket and threads. Unfortunately my approach does not work at all.
I wrote a simple case test: client and server. The client sends 2200
packets within 0.137447118759 secs. The tcpdump received 2189 packets,
which is not bad at all.
But the server only handles 700 -- 870 packets, when it is non-
blocking, and only 670 ? 700 received with blocking sockets.
The client and the server are working within the same local network
and tcpdump shows pretty correct amount of packets received.
I included a bit of the code of the UDP server.
class PacketReceive(threading.Thread):
* * def __init__(self, tname, socket, queue):
* * * * self._tname = tname
* * * * self._socket = socket
* * * * self._queue = queue
* * * * threading.Thread.__init__(self, name=self._tname)
* * def run(self):
* * * * print ''Started thread: '', self.getName()
* * * * cnt = 1
* * * * cnt_msgs = 0
* * * * while True:
* * * * * * try:
* * * * * * * * data = self._socket.recv(512)
* * * * * * * * msg = data
* * * * * * * * cnt_msgs += 1
* * * * * * * * total += 1
* * * * * * * * # self._queue.put(msg)
* * * * * * * * print *''thread: %s, cnt_msgs: %d'' % (self.getName(),
cnt_msgs)
* * * * * * except:
* * * * * * * * pass
I was also using Queue, but this didn''t help neither.
Any idea what I am doing wrong?
I was reading that Python socket modules was causing some delays with
TCP server. They recomended to set up *socket option for nondelays:
"sock.setsockopt(SOL_TCP, TCP_NODELAY, 1) ". I couldn''t find any
similar option for UDP type sockets.
Is there anything I have to change in socket options to make it
working faster?
Why the server can''t process all incomming packets? Is there a bug in
the socket layer? btw. I am using Python 2.5 on Ubuntu 8.10.
Cheers
KStupid question: did you try removing the print (e.g. printing once
every 100 messages) ?
Ciao
----
FB
这篇关于编写快速UDP服务器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!