python中的多线程其实并不是真正的多线程,如果想要充分地使用多核cpu的资源,在python中大部分情况需要使用多进程。
进程与线程的使用有很多相似之处,有关线程方面的知识请参考href="https://www.cnblogs.com/sfencs-hcy/p/9721362.HTML" target="_blank">https://www.cnblogs.com/sfencs-hcy/p/9721362.HTML
1.进程的创建
<span >def<span > func(msg):<span >print<span >(msg)
<span >print(<span >"<span >这是一个进程<span >"<span >)
<span >if <span >name==<span >"<span >main<span >"<span >:
p=multiprocessing.Process(target=func,args=(<span >"<span >hello world<span >"<span >,))
p.start()
以继承类的方式创建进程
==</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> run(self): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://m.jb51.cc/tag/zheli/" target="_blank" >这里</a>是将threading.Thread中的run<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" >方法</a><a href="https://m.jb51.cc/tag/jinxing/" target="_blank" >进行</a>了重载</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"><a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a> is %d</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span>%<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>,self.age))
<span >if <span >name==<span >"<span >main<span >"<span >:t=Myprocessing(<span >"<span >sfencs<span >",19<span >)
t.start()
2.进程的并行
===</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> run(self): </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(self.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>) time.sleep(self.second) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(self.age)
<span >if <span >name==<span >"<span >main<span >"<span >:time_begin=<span >time.time()
p1</span>=Myprocessing(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">sfencs</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span>,19,2<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)p2</span>=Myprocessing(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Tom</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span>,25,5<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)p1.start()p2.start()p1.join()p2.join()time_end</span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">time.time()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">print</span>(time_end-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">time_begin)
<span >'''<span >Tom
19
25
5.198107481002808
<span >'''
join的用法和线程相同
3.守护进程
守护进程与守护线程的原理相同,只不过设置守护进程的方式为p.daemon=True
4.lock
lock的作用同多线程,实现方式有两种
<span >def<span > func2(lock,f):with lock:
fs=open(f,<span >'<span >a+<span >'<span >)
fs.write(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Lockd acquired via with\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">'</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) fs.close()
<span >def<span > func1(lock,f):
lock.acquire()
fs=open(f,<span >'<span >a+<span >'<span >)
fs.write(<span >'<span >Lock acquired directly\n<span >'<span >)
fs.close()
lock.release()
<span >if <span >name==<span >"<span >main<span >"<span >:
lock=<span >multiprocessing.Lock()
f = <span >"<span >file.txt<span >"<span >
p1=multiprocessing.Process(target=func2,args=<span >(lock,f,))
p2=multiprocessing.Process(target=func1,))
p1.start()
p2.start()
p1.join()
p2.join()
与线程不同的是,这里lock是以参数方式传递,因为不同的进程并不能共享资源
5.Semaphore
用来控制对共享资源的最大访问数量
<span >def<span > func(s,i):s.acquire()
<span >print(multiprocessing.current_process().name + <span >"<span >acquire<span >"<span >);
time.sleep(2<span >)
<span >print(multiprocessing.current_process().name + <span >"<span >release\n<span >"<span >);
s.release()
<span >if <span >name == <span >"<span >main<span >"<span >:
s = multiprocessing.Semaphore(2<span >)
<span >for i <span >in range(5<span >):
p = multiprocessing.Process(target = func,args=(s,2<span >))
p.start()
6.event与线程用法相同
7.队列
有一个专门属于多进程的队列multiprocessing.Queue
<span >def<span > writer(q):q.put(<span >"<span >hello world<span >"<span >)
<span >def<span > reader(q):
<span >print<span >(q.get())
<span >if <span >name == <span >"<span >main<span >"<span >:
q =<span > multiprocessing.Queue()
pwriter=multiprocessing.Process(target=writer,args=<span >(q,))
preader = multiprocessing.Process(target=reader,))
pwriter.start()
preader.start()
8.管道pipe
Pipe方法返回(conn1,conn2)代表一个管道的两个端。Pipe方法有duplex参数,如果duplex参数为True(默认值),那么这个管道是全双工模式,也就是说conn1和conn2均可收发。duplex为False,conn1只负责接受消息,conn2只负责发送消息。
<span >def<span > sender(p):p.send(<span >"<span >hello world<span >"<span >)
<span >def<span > receiver(p):
<span >print<span >(p.recv())
<span >if <span >name == <span >"<span >main<span >"<span >:
p =<span > multiprocessing.Pipe()
psender=multiprocessing.Process(target=sender,args=<span >(p[0],))
preceiver = multiprocessing.Process(target=receiver,args=(p[1<span >],))
psender.start()
preceiver.start()
9.manager
manager实现进程之间数据共享
<span >def<span > func(List1,d,i):List1[i]=<span >i
d[<span >"<span >a<span >"]=<span >i
<span >if <span >name == <span >"<span >main<span >"<span >:
with multiprocessing.Manager() as manager:
List1=manager.List(range(5,10<span >))
d=<span >manager.dict()
pList=<span >[]
<span >for i <span >in range(5<span >):
p=multiprocessing.Process(target=func,args=<span >(List1,i))
pList.append(p)
p.start()
<span >for i <span >in<span > pList:
i.join()
<span >print<span >(List1)
<span >print(d)
未完
总结以上是内存溢出为你收集整理的python基础学习21----进程全部内容,希望文章能够帮你解决python基础学习21----进程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)