进程vs线程
2020-12-13 14:10
标签:read work pre 创建 multi main res reading range 子线程 is running 主子线程 is end 主子进程 is running 子进程 is end 进程vs线程 标签:read work pre 创建 multi main res reading range 原文地址:https://www.cnblogs.com/ledgua/p/11552387.html进程vs线程
进程vs线程创建速度
from threading import Thread
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is end')
if __name__ == '__main__':
t = Thread(target=task,args=('子线程',))
p = Process(target=task,args=('子进程',))
# t.start()
p.start()
print('主')
开启子线程的打印效果:
开启子进程打印效果:
进程和线程的创建速度
多线程vs多进程
计算密集型
from threading import Thread
from multiprocessing import Process
import time
# 计算密集型
# def work1():
# res=0
# for i in range(100000000): #1+8个0
# res*=i
#
# if __name__ == '__main__':
# t_list = []
# start = time.time()
# for i in range(4):
# # t = Thread(target=work1)
# t = Process(target=work1)
# t_list.append(t)
# t.start()
# for t in t_list:
# t.join()
# end = time.time()
# # print('多线程',end-start) # 多线程 15.413789510726929
# print('多进程',end-start) # 多进程 4.711405515670776
IO密集型
# # io密集型
# def work1():
# x = 1+1
# time.sleep(5)
#
#
# if __name__ == '__main__':
# t_list = []
# start = time.time()
# for i in range(4):
# t = Thread(target=work1)
# # t = Process(target=work1)
# t_list.append(t)
# t.start()
# for t in t_list:
# t.join()
# end = time.time()
# print('多线程',end-start) # 多线程 5.002625942230225
# # print('多进程',end-start) # 多进程 5.660863399505615
上一篇:Windows UDP socket recvfrom返回10054错误的解决办法
下一篇:数据统计分析SYSTAT.v13.1.Win32_64 2CD+IBM.SPSS.Amos.v22 1CD 统计分析