使用服务器和客户端线程时,Python TCP客户端冻结

时间:2020-08-08 03:45:28

标签: python multithreading sockets tcp

我一直在尝试实现一个简单的4节点网络,其中每个节点向另一个节点发送一个“ hello”。我正在使用两个线程,用于发送问候的客户端线程和用于接收问候的服务器线程。当我在AWS上运行节点时,客户端总是在发送1或2个问候后陷入困境。我不确定代码会冻结什么,但是我看到它冻结在s.connect()上。 使用键盘中断杀死进程时,我已附加了追溯。每次创建时,我都尝试过重用套接字名称。我也尝试过有或没有``sleep`'',但结果相同。

请帮助

N_NODES = 4
def serverSock(MY_IP, MY_PORT):
    s  = socket.socket ( socket.AF_INET , socket.SOCK_STREAM )
    s.bind ( ( '', MY_PORT ) )
    s.listen ( N_NODES)
    for peer in range ( N_NODES-1 ) :
        try:
            peer_con , peer_addr = s.accept ( )
            data_received = recv_data ( peer_con)
        except Exception as e: print(e)

def sendId2peers(nid ):
    for node_index in range ( N_NODES) :
        if (MY_IP != NODE_IP[node_index]) :
            try:
                connection_retries = 5 
                while connection_retries > 0:
                    s = socket.socket ( socket.AF_INET , socket.SOCK_STREAM )
                    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

                    try:
                        s.connect ( ( NODE_IP[node_index], BASE_PORT+ node_index ) ) 
                    except Exception as e:
                   
                        sleep(0.2)
                        connection_retries -= 1
                    else:
                       
                        data_to_send= {
                                    'my_id':str(nid),
                                    'payload': "hello"
                                        }
                        data_to_send = json.dumps(data_to_send)
                        
                        send_data(s, data_to_send)
                        s.close()
                        break
            except Exception as e: print("Error while sending hello to node_id:", node_index, e)
    return


def node_thread(nid):
    print("Starting Node: ", nid)
    MY_PORT = BASE_PORT + nid
    DPRF_PORT = MY_PORT + 1000

    node_server_thread = threading.Thread(target = serverSock, args = (MY_IP, MY_PORT))
    node_server_thread.start()


    node_client_thread = threading.Thread(target = sendId2peers, args = (nid, ))
    node_client_thread.start()


    node_client_thread.join()
    node_server_thread.join()

    print("PHASE0: Finished the first handshake with all the nodes")

当我使用Ctrl + C终止程序时,这是我得到的回溯:

File "send_receive_hello.py", line 155, in <module>
    node_thread(int(args.iden))
  File "send_receive_hello.py", line 129, in node_thread
    node_client_thread.join()
  File "/usr/lib/python3.6/threading.py", line 1056, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.6/threading.py", line 1072, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt
^CException ignored in: <module 'threading' from '/usr/lib/python3.6/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 1294, in _shutdown
    t.join()
  File "/usr/lib/python3.6/threading.py", line 1056, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.6/threading.py", line 1072, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):

0 个答案:

没有答案