python3 线程比进程的开销小
作者:互联网
# -*- coding: utf-8 -*- from threading import Thread def func(name): print("子线程: %s" %name) if __name__ == '__main__': t = Thread(target=func, args=("lily",)) t.start() print("主线程") # 子线程: lily # 主线程
# -*- coding: utf-8 -*- from multiprocessing import Process def func(name): print("子进程: %s" %name) if __name__ == '__main__': p = Process(target=func, args=("lily",)) p.start() # 这里需要资源开销比线程大,所以先打印主进程 print("主进程") # 主进程 # 子进程: lily
第二种开启线程的方法(自定义类,继承Thread类)
# -*- coding: utf-8 -*- from threading import Thread class MyThread(Thread): def run(self): # 必须有的方法 print("子线程: %s" % self.name) if __name__ == '__main__': t = MyThread() t.start() print("子线程") # 子线程: Thread-1 # 子线程
标签:__,开销,name,Thread,线程,print,lily,python3 来源: https://www.cnblogs.com/lilyxiaoyy/p/11025214.html