由于套接字功能,while循环未终止

时间:2018-07-09 14:35:58

标签: python loops sockets while-loop

我正在尝试在此程序中运行两个while循环。 第一个while循环最多需要运行50秒,但是此循环在50秒后不会退出(50秒后不会终止),它只会继续。我在单独的程序中验证了计时器,在该程序中计时器可以正常工作,但在我的程序中计时器不起作用。

import array
import time 
import socket  
import os,sys
import concurrent.futures
import struct 

Nmax_Per_hop=100
hello_Broadcast_Period=40
NeighborDiscovery_Time_Interval=50
MyNeighborSet_ip=[]
all_ip=[]
a=10
b=0
start_time=time.time()
My_ip='192.168.1.1'
#############################################################################
def broadcast_hello():           # (send 'Hello message')
    ip1 = "192.168.1.254"
    print ("[tx]------>hello to:"+ ip1)
    PORT = 12345

    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
        s.sendto (b'Hello',('192.168.1.254',PORT))
##########################################################################

def received_hello():      
    PORT = 12345
    ip=[]
    recv_message_hello=[]
    global a
    s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    s.bind(('',PORT))
    message=s.recvfrom(4096)
    print ("before the ip") 
    ip=message[1][0]
    if (ip not in all_ip):
        all_ip.append(ip)
        print (all_ip)

    recv_message=message[0]  
    if not recv_message in recv_message_hello:
        recv_message_hello=recv_message

    if (ip==My_ip):
        pass
    elif ip not in MyNeighborSet_ip :
        MyNeighborSet_ip.append(ip)
    else:
        print ("Already in List")

    print ("[N-Set]------------:",MyNeighborSet_ip)

##########################################################################

temp = concurrent.futures.ThreadPoolExecutor (max_workers=2)

此处此循环运行不正常

while(((time.time()-start_time)<NeighborDiscovery_Time_Interval) and (len(MyNeighborSet_ip) <= Nmax_Per_hop)):

    if (time.time()-start_time<hello_Broadcast_Period):
        temp.submit(broadcast_hello)
    try:
        temp.submit(received_hello)
    except:
        print("###################")

print ("IP of this node is: ",My_ip)
print ("[N-Set]------------:",MyNeighborSet_ip)

while (b<10):
    print ("dfdfdfdfdfdfdfdf")
    b+=1

有人可以帮助我解决此问题吗?

0 个答案:

没有答案