Network programming as much as a thread - open multiple threads in two ways

Network programming as much as a thread - open multiple threads in two ways

A, threading module

multiprocess module completely mimics the threading module interfaces, both at the level of use, there are a lot of similarities, and thus not described in detail.

Second, the two ways open thread

method one

from threading import Thread
import time
def sayhi(name):
    time.sleep(2)
    print('%s say hello' %name)
if __name__ == '__main__':
    t=Thread(target=sayhi,args=('egon',))
    t.start()
    print('主线程')

Second way

from threading import Thread
import time
class Sayhi(Thread):
    def __init__(self,name):
        super().__init__()
        self.name=name
    def run(self):
        time.sleep(2)
        print('%s say hello' % self.name)
if __name__ == '__main__':
    t = Sayhi('egon')
    t.start()
    print('主线程')

Guess you like

Origin www.cnblogs.com/Kwan-C/p/11589383.html