網絡編程之多線程——開啓多線程的兩種方式

網絡編程之多線程——開啓多線程的兩種方式

1、threading模塊

multiprocess模塊徹底模仿了threading模塊的接口,兩者在使用層面,有很大的類似性,於是再也不詳細介紹了。python

2、開啓線程的兩種方式

方式一編程

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('主線程')

方式二網絡

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('主線程')
相關文章
相關標籤/搜索