进程
进程是资源分配的最小单位( 内存、cpu、网络、io)
一个运行起来的程序就是一个进程
进程具有独立的内存空间,所以没有办法相互通信
为什么需要进程池
from multiprocessing import Pool
import os, time
def worker(i):
print('第%d次打印,我爱你中国'%(i))
time.sleep(1)
if __name__ == '__main__':
po = Pool(20) # 定义一个进程池,最大进程数20
for i in range(1,101):
# Pool().apply_async(要调用的目标,(传递给目标的参数元祖,))
# 每次循环将会用空闲出来的子进程去调用目标
po.apply_async(worker, args = (i,))
print("----start----",end='\n')
po.close() # 关闭进程池,关闭后po不再接收新的请求
po.join() # 等待po中所有子进程执行完成,必须放在close语句之后
print("-----end-----")
创建进程
1、导包 import multiprocessing
2、创建任务 函数
3、用multiprocessing.Process(target=函数名)
4、开启进程 start()
5、join()
6、terminate() 立即结束子进程
import os
from multiprocessing import Process
import time
num = 100
def func1():
global num
num +=50
print(num,'进程号',os.getpid())
def func2():
global num
num += 500
print(num,'进程号',os.getpid())
if __name__ == '__main__':
print('kaishi')
print(num)
p1 = Process(target=func1)
p2 = Process(target=func2)
p1.start()
p2.start()
# p1.terminate()
# p2.terminate()
p1.join()
p2.join()
print('结束')
from threading import Thread
def func1(man):
print('我是',man)
def func2(name):
print('我是',name)
if __name__ == '__main__':
print('主线程开始')
t1 = Thread(target=func1,args=('aa1',))
t2 = Thread(target=func2,kwargs={'name':'aa'})
t1.start()
t2.start()
t1.join()
t2.join()
print('主线程结束')
def func():
print('子线程开始')
for i in range(5):
print('线程',i)
time.sleep(0.1)
print('子线%s程结束'%i)
if __name__ == '__main__':
print('主线程开始')
t =Thread(target=func,daemon=True)
# t = Thread(target=func)
t.setDaemon(True)
t.start()
t.join()
print('主线程结束')
from threading import Thread
import threading
num = 0
lock1 = threading.Lock() #创建全局互斥锁
def func1():
lock1.acquire() # 上锁
global num #修改全局变量
for i in range(1000000):
num +=1
print('sum1',num)
lock1.release() #解锁
def func2():
lock1.acquire() # 上锁
global num #修改全局变量
for i in range(1000000):
num +=1
print('sum2',num)
lock1.release() #解锁
if __name__ == '__main__':
t1 = Thread(target=func1)
t2 = Thread(target=func2)
t1.start()
t2.start()
'''
协程
实现协程的方式
1、yield
2、greenlet
3、gevent
'''
import time
import greenlet
import gevent
import urllib.request
from gevent import monkey
# 用yield 实现协程
# def work1():
# while 1:
# print('真可爱')
# time.sleep(1)
# yield
# def work2():
# while 1:
# print('真坏')
# time.sleep(1)
# yield
#
# g1 = work1()
# g2 = work2()
# while 1:
# next(g1)
# next(g2)
# def consumer(): # consumer 消费者
# r = ''
# while True:
# n = yield r
# print('[消费者] Consuming %s...' % n)
#
#
# def produce(c): # produce 生产者
# c.send(None)
# for n in range(1, 6):
# print('[生产者] Producing %s...' % n)
# r = c.send(n)
# c.close()
#
#
# c = consumer()
# produce(c)
#-----用greenlet实现协程
# def work1():
# for i in range(5):
# print('姬小飞2000秒')
# time.sleep(0.2)
# g2.switch()
#
#
# def work2():
# for i in range(5):
# print('波老师')
# time.sleep(0.2)
# g1.switch()
#
#
# g1 = greenlet.greenlet(work1)
# g2 = greenlet.greenlet(work2)
# g1.switch() # 开启并切换到work1
# import gevent
#
#
# def jxf():
# print('姬小飞开始')
# gevent.sleep(4)
# print('姬小飞结束')
#
#
# def jhc():
# print('姬宏辰开始')
# gevent.sleep(1)
# print('姬宏辰结束')
#
#
# def lza():
# print('刘再昂开始')
# gevent.sleep(7)
# print('刘再昂结束')
#
#
# gevent.joinall([
# gevent.spawn(jxf),
# gevent.spawn(jhc),
# gevent.spawn(lza)
# ])```