为什么这些 类 中的第二种方法只要不被调用就可以执行它们的功能?

Why second method of these classes are performing their functions as long as they are not called?

这里我有服务器和客户端可以连接在一起并发送 message.but 第二种方法*(def run(self):)* 服务器和客户端中的这些 类 都没有调用为什么要发送它们并接收消息?(为什么这些方法不调用它们就可以工作?)


使用套接字和线程的服务器代码:

from threading import Thread
import socket


class SendingThread(Thread):
    def __init__(self, mySocket):
        Thread.__init__(self)
        self.mySocket = mySocket

    def run(self):
        # write code to send data continuously
        while True:
            data = input()
            self.mySocket.send(bytes(data, 'utf-8'))


class ReceivingThread(Thread):
    def __init__(self, mySocket):
        Thread.__init__(self)
        self.mySocket = mySocket

    def run(self):
        # write code to receive data continuously
        while True:
            msg = self.mySocket.recv(1024)
            print(msg.decode('utf-8'))


# create a socket object
s = socket.socket(
    socket.AF_INET, # internet address family => IP v4
    socket.SOCK_STREAM # TCP
)
# bind socket with a port number
s.bind(('127.0.0.1', 2010))
# keep System_1 in listening mode
s.listen()
# accept the incoming connection request
mySocket, address = s.accept()
# create a thread to send data
sendThread = SendingThread(mySocket)
# create an another to receive data
receiveThread = ReceivingThread(mySocket)
# start both threads
sendThread.start()
receiveThread.start()

使用套接字和线程的客户端代码:

from threading import Thread
import socket


class MySendingThread(Thread):
    def __init__(self, mySocket):
        Thread.__init__(self)
        self.mySocket = mySocket

    def run(self):
        # write code to send data to System_1
        while True:
            data = input()
            self.mySocket.send(bytes(data, 'utf-8'))


class MyReceivingThread(Thread):
    def __init__(self, mySocket):
        Thread.__init__(self)
        self.mySocket = mySocket

    def run(self):
        # write code to receive data from System_1
        while True:
            msg = self.mySocket.recv(1024)
            print(msg.decode('utf-8'))


# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# send a connection request
s.connect(('127.0.0.1', 2010))
# create a thread to send data => System_1
mySendThread = MySendingThread(s)
# create a thread to receive data from System_1
myReceiveThread = MyReceivingThread(s)
# start both threads
mySendThread.start()
myReceiveThread.start()
服务器中的

SendingThreadReceivingThread 以及客户端中的 MySendingThreadMyReceivingThread 继承自 threading.Thread

有一个look at the docs:

The Thread class represents an activity that is run in a separate thread of control. There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass. No other methods (except for the constructor) should be overridden in a subclass. In other words, only override the __init__() and run() methods of this class.

Once a thread object is created, its activity must be started by calling the thread’s start() method. This invokes the run() method in a separate thread of control.

当您调用 start() 时,它会调用您在子 class.

中覆盖的相应 run() 方法