python3 线程比进程的开销小
2020-12-13 02:03
标签:开启 打印 RoCE name span get 资源 定义 process 第二种开启线程的方法(自定义类,继承Thread类) python3 线程比进程的开销小 标签:开启 打印 RoCE name span get 资源 定义 process 原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11025214.html# -*- 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
# -*- 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
# 子线程