pythonmultithreadingserver

Threading has no attribute Thread, no files named threading.py


I'm coding a simple client/server setup. The server seems to start fine, but when I try to connect the client I get a message saying "Threading has no attribute Thread." Then the server stops.

The only suggestion I've found doesn't work for me ... I have no files named threading.py, and the print(threading.__file__) statement shows the correct file path for the threading module.

I've left out functions that don't use the threading module for this example.

import socket
import threading
print(threading.__file__)

HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = "utf-8"
DISCONNECT_MESSAGE = "!"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

...

def start():
    # Listen for and accept connections
    server.listen()
    print(f"[LISTENING] Server is listening on {SERVER}")
    while True:
        conn, addr = server.accept()
        t = threading.thread(target=handle_client, args=(conn, addr))
        t.start()
        print(f'[ACTIVE CONNECTIONS] {threading.activeCount() - 1}')

print('Server is starting ...')
start()

Solution

  • The correct form is threading.Thread, not threading.thread.

    Otherwise, thank you for having researched first, and looked if you didn't have a threading.py file shadowing Python's: that is indeed the usual cause of this type of error. In this case, it is just a typo, though.