一 threading模块介绍
multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性
二 开启线程的两种方式
方式一
from threading import Thread import time import random def running(name): print("%s is running" % name) time.sleep(random.randint(1,3)) print("%s running end" % name) if __name__ == "__main__": t1 = Thread(target=running,args=("camz",)) t1.start()
camz is running camz running end
方式二
from threading import Thread import time import random class Say(Thread): def __init__(self, name): super().__init__() self.name = name def run(self): print("%s is running" % self.name) time.sleep(random.randint(1,3)) print("%s running end" % self.name) if __name__ == "__main__": t1 = Say("cmz") t1.start() print(‘主线程‘)
cmz is running 主线程 cmz running end