蟒蛇;(Python; Troubles controlling dead sockets through select)
我有一些代码将连接到主机,除了监听传入的数据,直到客户端关闭或主机发送关闭语句。 为此我的代码运作良好。
但是,当主机在没有发送关闭语句的情况下死亡时,我的客户端会一直按预期继续侦听传入数据。 为了解决这个问题,我每隔foo秒就进行一次套接字超时,并开始检查连接是否存活。 从Python 套接字 howto我发现了这个:
选择的一个非常令人讨厌的问题:如果那些输入列表中的插座中的某个地方已经死了一个令人讨厌的死亡,那么选择将失败。 然后你需要遍历所有这些列表中的每一个该死的套接字并执行select([sock],[],[],0)直到找到坏的套接字。 超时0意味着它不会花费很长时间,但它很难看。
# Example code written for this question. from select import select from socket include socket, AF_INET, SOCK_STREAM socket = socket(AF_INET, SOCK_STREAM) socket.connect(('localhost', 12345)) socklist = [socket,] attempts = 0 def check_socklist(socks): for sock in socklist: (r, w, e) = select([sock,], [], [], 0) ... ... ... while True: (r, w, e) = select(socklist, [], [], 60) for sock in r: if sock is socket: msg = sock.recv(4096) if not msg: attempts +=1 if attempts >= 10: check_socket(socklist) break else: attempts = 0 print msg
本文提出了三个问题。
- 我被教导要检查连接是否存活,必须写入套接字并查看响应是否返回。 如果没有,则必须假设连接已经死亡。 在文中它说要检查错误的连接,每个套接字一个,将它传递给select的第一个参数并将超时设置为零。 这将如何确认插座是否已经死?
- 为什么不通过尝试写入套接字来测试套接字是死还是活?
- 当连接处于活动状态并且已经死亡时,我在寻找什么? 选择将立即超时,因此没有数据将证明什么。
我意识到像
gevent
,asyncore
和twisted
这样的库可以帮助我解决这个问题,但是我选择这样做是为了更好地理解正在发生的事情,并且能够更好地控制我的源头。I have some code which will connect to a host and do nothing but listen for incoming data until either the client is shut down or the host send a close statement. For this my code works well.
However when the host dies without sending a close statement, my client keeps listening for incoming data forever as expected. To resolve this I made the socket timeout every foo seconds and start the process of checking if the connection is alive or not. From the Python socket howto I found this:
One very nasty problem with select: if somewhere in those input lists of sockets is one which has died a nasty death, the select will fail. You then need to loop through every single damn socket in all those lists and do a select([sock],[],[],0) until you find the bad one. That timeout of 0 means it won’t take long, but it’s ugly.
# Example code written for this question. from select import select from socket include socket, AF_INET, SOCK_STREAM socket = socket(AF_INET, SOCK_STREAM) socket.connect(('localhost', 12345)) socklist = [socket,] attempts = 0 def check_socklist(socks): for sock in socklist: (r, w, e) = select([sock,], [], [], 0) ... ... ... while True: (r, w, e) = select(socklist, [], [], 60) for sock in r: if sock is socket: msg = sock.recv(4096) if not msg: attempts +=1 if attempts >= 10: check_socket(socklist) break else: attempts = 0 print msg
This text creates three questions.
- I was taught that to check if a connection is alive or not, one has to write to the socket and see if a response returns. If not, the connection has to be assumed it is dead. In the text it says that to check for bad connections, one single out each socket, pass it to select's first parameter and set the timeout to zero. How will this confirm that the socket is dead or not?
- Why not test if the socket is dead or alive by trying to write to the socket instead?
- What am I looking for when the connection is alive and when it is dead? Select will timeout at once, so having no data there will prove nothing.
I realize there are libraries like
gevent
,asyncore
andtwisted
that can help me with this, but I have chosen to do this my self to get a better understanding of what is happening and to get more control over the source my self.
原文:https://stackoverflow.com/questions/19795529
最满意答案
我想你正在考虑modulo ( php modulo operator
%
)? 它在执行除法后提供余数。 例如:师:
10 / 3 = 3.333...
模:
10 % 3 = 1
在编程使用中,您可以使用第一个(除法)操作的楼层来获取完整组的数量,并使用模数来获取最后一组的大小。
例如,如果你以14开头:
3组有多少组?
floor(14 / 3) = 4
最后一组有多少人?
14 % 3 = 2
I think you are thinking of modulo (php modulo operator
%
)? It provides the remainder after performing division. For example:Division:
10 / 3 = 3.333...
Modulo:
10 % 3 = 1
In programming use, you can use the floor of the first (division) operation to get the number of full groups, and the modulus to get the size of the last group.
For example, if you start with 14:
How many groups of 3?
floor(14 / 3) = 4
How many in last group?
14 % 3 = 2
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
import math x = 1234.5678 math.modf(x) # (0.5678000000000338, 1234.0) Use math.modf: import math x = 1234.5678 math.modf(x) # (0.5678000000000338, 1234.0)
-
处理大量数字(Handling large groups of numbers)[2022-03-22]
阅读有关记忆的内容 。 关键的见解是,如果你有一个序列从A开始,其长度为1001,然后你得到一个产生A的序列B,你就不要再重复所有这些工作了。 Have a read about memoization. The key insight is that if you've got a sequence starting A that has length 1001, and then you get a sequence B that produces an A, you don't to repeat a ... -
这是我的第一次拍摄(来自评论)。 我会编辑这篇文章,因为我会有更好的想法。 算法: Input (a) a list L (b) a number x, the maximum gap 1) Sort the list 2) Take as many elements from the list as you can without exceeding the gap 3) Create a new group 4) If there are no more elements in the list, yo ...
-
将范围拆分为X组(Splitting a range into X groups)[2022-09-21]
喜欢这个? def split_ranges(amount, max) (0...amount).collect{|i| (i * max / amount)...((i+1) * max / amount)} end p split_ranges(3, 46000000) 输出: [0...15333333, 15333333...30666666, 30666666...46000000] 编辑:(OP请求) def split_ranges(amount, max) (0...a ... -
将值均匀拆分为组(Splitting values into groups evenly)[2021-09-29]
我会这样做: 添加所有值,让我们调用这个S. 将S除以列数,我们称之为M. 使用背包算法(例如http://search.cpan.org/~andale/Algorithm-Knapsack-0.02/lib/Algorithm/Knapsack)找到一组其值为M或尽可能接近M的值。下午 (只是一个快速的谷歌背包)) 取一组值的总和并从S中减去它,我们称之为T. 将T除以列数减1 并重复该算法 I would do it like this: add all the values, let's call ... -
编程概念:什么是将一个数字分成其他整数的组?(Programming Concept: What is splitting a number into groups of other whole numbers?)[2022-05-28]
我想你正在考虑modulo ( php modulo operator % )? 它在执行除法后提供余数。 例如: 师: 10 / 3 = 3.333... 模: 10 % 3 = 1 在编程使用中,您可以使用第一个(除法)操作的楼层来获取完整组的数量,并使用模数来获取最后一组的大小。 例如,如果你以14开头: 3组有多少组? floor(14 / 3) = 4 最后一组有多少人? 14 % 3 = 2 I think you are thinking of modulo (php modulo o ... -
将数量分割一定数量(Splitting up number by certain amount)[2022-01-27]
这应该产生你想要的 n = 8202; a = [0:4001:n; [4000:4001:n-1 n]]' 回报 a = 0 4000 4001 8001 8002 8202 This should produce what you want n = 8202; a = [0:4001:n; [4000:4001:n-1 n]]' returns a = 0 4000 ... -
编程概念(Programming concept)[2022-03-05]
这是一个快速的脚本,可能会让你靠近那里: MAXPOINTS = 10 JUNKWORDS={"trouble":6,"solution":5,"charge":3,"virus":7} fil = open("filnamne.txt", "r") foundwords = {} points = 0 for word in fil.read().split(): if word in JUNKWORDS: if word not in foundwords: ... -
将数字间隔分组(Split intervals of numbers into groups)[2022-05-25]
您需要考虑某种方式将序列转换为相应的组。 我刚才从其他用户那里学到了这个技巧。 通过使用贯穿所有记录的ROW_NUMBER ,您可以通过从该行号中减去同一记录上的value来计算组密钥。 如果values是连续的,则减法结果不会发生变化(因此产生相同的组密钥)。 否则,组密钥将跳转到下一个(较小)值。 每次跳跃时,组密钥都会变小。 这是查询: select min(value) min, max(value) max from (select value, ROW_NUMBER() over (order ...