Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

multithreading - Python listen socket thread freezes on accept

Im working on a simulated network, however often times while trying to run the code, one of the threads managing a listen socket just ends up being stuck on the accept command, I am using visual studio and when i try to debug the situation i end up seeing the thread (when running the same network it will fail on the same thread too) is stuck on the accept statement, but i cannot see any of the thread's variables and i only see "Unable to find thread to evaluate variable reference". The weirder thing is that this is not very replicatable as the run works on the first try but fails if i try the same network size again afterwards.

    reader = open('input_vertex_' + str(ID) + '.txt')
    reader = [line.rstrip() for line in reader]
    n = 0
    port = None
    num = 0
    Neighbors = list()
    flag = True
    counter = 0
    TCP = 0
    for line in reader:
        if line == '*':
            break
        if counter == 0:
            n = int(line)
        elif counter == 1:
            port = int(line)
        elif counter == 2:
            num = int(line)
        else:
            if flag:
                TCP = int(line)
                flag = False
            else:
                Neighbors.append((line, TCP))
                flag = True
        counter += 1
    vert = v(ID, n, port, num, Neighbors)

    messages_sent = 0
    for neighbor in Neighbors:
        send_message(str(ID) + ' ' + str(1), neighbor[1], neighbor[0])
    flag_1 = True
    while True:
        sock, addr = vert.listen_socket.accept()
        t = Thread(target=receive, args=(sock, vert,))
        t.daemon = True
        t.start()

The code first reads from a file to know its own port and neighbors and other data for testing. v is a vertex object that holds all these parameters, and its constructor also holds a listen socket:

    self.listen_socket = socket(AF_INET, SOCK_STREAM)
    self.listen_socket.bind(('127.0.0.1', port))
    self.listen_socket.listen()

send message is an external function that does sendall for for the target ip and port. The weird thing is that it does sometimes work and sometimes doesnt, but whenever i do run the debugger on VS, i see that it is always the same thread that freezes, I would be glad if anyone could explain why is such a freeze happening and how could it be prevented.

question from:https://stackoverflow.com/questions/65863601/python-listen-socket-thread-freezes-on-accept

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...