1.进程的创建
import multiprocessing
def func(msg):
print(msg)
print('This is a processing')
if __name__ == '__main__':
p = multiprocessing.Process(target=func,args=('test',))
p.start()
2.进程的并行
import multiprocessing
def func(msg):
print(msg)
print('This is a processing')
if __name__ == '__main__':
p1 = multiprocessing.Process(target=func,args=('test1',))
p2 = multiprocessing.Process(target=func,args=('test2',))
p1.start()
p2.start()
p1.join()
p2.join()
3.进程锁的两种使用方法
import multiprocessing
from datetime import datetime,timedelta
#with用锁
def func2(lock,f):
with lock:
print(datetime.now())
#lock.acquire()用锁
def func1(lock,f):
lock.acquire()
print(datetime.now())
lock.release
if __name__ == '__main__':
lock = multiprocessing.Lock()
f = 'file.txt'
p1 = multiprocessing.Process(target=func2,args=(lock,f))
p2 = multiprocessing.Process(target=func1,args=(lock,f))
p1.start()
p2.start()
p1.join()
p2.join()
3.设置资源的访问数量
import multiprocessing
from datetime import datetime,timedelta
#with用锁
def func2(s,lock,f):
with s:
with lock:
print(func2.__name__ +' ' +datetime.now().strftime('%Y-%m-%d %H-%S-%M'))
#lock.acquire()用所
def func1(lock,f):
lock.acquire()
print(func1.__name__ +' ' + datetime.now().strftime('%Y-%m-%d %H-%S-%M'))
lock.release
if __name__ == '__main__':
s = multiprocessing.Semaphore(2) # 设置只能访问两次
for i in range(5):
lock = multiprocessing.Lock()
f = 'file.txt'
p1 = multiprocessing.Process(target=func2,args=(s,lock,f))
p2 = multiprocessing.Process(target=func1,args=(lock,f))
p1.start()
p2.start()
p1.join()
p2.join()
4.Processing 队列设置
import multiprocessing
def write(q):
q.put('Hello,World')
def read(q):
print(q.get())
if __name__ == '__main__':
q = multiprocessing.Queue()
qwriter = multiprocessing.Process(target=write,args=(q,))
qreader = multiprocessing.Process(target=read,args=(q,))
qwriter.start()
qreader.start()
5.管道pipe
pipe返回(conn1,conn2)代表管道的两端,可以用[0]、[1]调用;
duplex参数如果设为True,则conn1和conn2都可以收发;
duplex参数如果设为False,则conn1负责接收,conn2负责发送。
import multiprocessing
def sender(p):
p.send("hello world")
def receiver(p):
print(p.recv())
if __name__ == "__main__":
p = multiprocessing.Pipe()
psender=multiprocessing.Process(target=sender,args=(p[0],))
preceiver = multiprocessing.Process(target=receiver, args=(p[1],))
psender.start()
preceiver.start()
6.进程之间manager
manager实现进程之间数据共享
import multiprocessing
def func(list1,d,i):
list1[i] = i
d['a'] = i
if __name__ == "__main__":
with multiprocessing.Manager() as manager:
list1 = manager.list(range(5,10))
d = manager.dict()
plist = []
for i in range(5):
p = multiprocessing.Process(target=func,args=(list1,d,i))
plist.append(p)
p.start()
p.join()
for i in plist:
print(i)
print(d)
print(list1)