日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python第10天(上)

發(fā)布時間:2025/4/16 python 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python第10天(上) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

multiprocessing包是Python中的多進(jìn)程管理包。與threading.Thread類似,它可以利用multiprocessing.Process對象來創(chuàng)建一個進(jìn)程。該進(jìn)程可以運行在Python程序內(nèi)部編寫的函數(shù)。該Process對象與Thread對象的用法相同,也有start(), run(), join()的方法。此外multiprocessing包中也有Lock/Event/Semaphore/Condition類 (這些對象可以像多線程那樣,通過參數(shù)傳遞給各個進(jìn)程),用以同步進(jìn)程,其用法與threading包中的同名類一致。所以,multiprocessing的很大一部份與threading使用同一套API,只不過換到了多進(jìn)程的情境

調(diào)用方法一:

import multiprocessing#多進(jìn)程包 import time def visit(name):time.sleep(1)print('hello', name,time.ctime())if __name__ == '__main__':p_list=[]for i in range(3):p = multiprocessing.Process(target=visit, args=('alvin',))#創(chuàng)建子進(jìn)程對象p_list.append(p)p.start()#開啟進(jìn)程for i in p_list:p.join()#與線程意義先疼痛print('end')

調(diào)用方法二:

class MyProcess(Process):def run(self):time.sleep(1)print ('hello', self.name,time.ctime())if __name__ == '__main__':p_list=[]for i in range(3):p = MyProcess()p.start()p_list.append(p)for p in p_list:p.join()print('end')

一:pid

class progress(multiprocessing.Process):def run(self):time.sleep(2)print("process name",self.name)print("parent process id",os.getppid(),time.time())print("process id",os.getpid(),time.time()) if __name__=="__main__":print("main process line")progress.run(progress)print("----------")t1=progress()t2=progress()t1.start()t2.start()#主進(jìn)程的parent process id 33440 1550129555.0609472 #主進(jìn)程的process id 21996 1550129555.0609472 #子進(jìn)程process name progress-1parent process id 21996 1550129557.234302process id 29496 1550129557.234302

二:方法與屬性

  • is_alive():返回進(jìn)程是否在運行。

  • join([timeout]):阻塞當(dāng)前上下文環(huán)境的進(jìn)程程,直到調(diào)用此方法的進(jìn)程終止或到達(dá)指定的timeout(可選參數(shù))。

  • start():進(jìn)程準(zhǔn)備就緒,等待CPU調(diào)度

  • run():strat()調(diào)用run方法,如果實例進(jìn)程時未制定傳入target,這star執(zhí)行t默認(rèn)run()方法。

  • terminate():不管任務(wù)是否完成,立即停止工作進(jìn)程

  • daemon:和線程的setDeamon功能一樣

  • name:進(jìn)程名字。

  • pid:進(jìn)程號

?一:進(jìn)程隊列queque

import multiprocessing,time def foo(parad):time.sleep(1)print("子進(jìn)程隊列id",id(parad))parad.put("name")parad.put({"name":"alex"}) if __name__=="__main__":p_list=[]parad=multiprocessing.Queue()#創(chuàng)建一個進(jìn)程隊列print("主進(jìn)程隊列",id(parad))for i in range(3):progress=multiprocessing.Process(target=foo,args=(parad,))progress.start()print(parad.get())print(parad.get())

二:管道pipe

  • pipe()函數(shù)返回一對通過一個雙向管道鏈接的鏈接對象  
  • parent_conn, child_conn = multiprocessing.Pipe()#創(chuàng)建一個管道對象
  • parent_conn.recv()#接收 parent_conn.send()#發(fā)數(shù)據(jù)
def foo(conn):time.sleep(1)conn.send("hellow father")print("子進(jìn)程收:",conn.recv())conn.close() if __name__=="__main__":parent_conn, child_conn = multiprocessing.Pipe()#創(chuàng)建一個管道對象progress=multiprocessing.Process(target=foo,args=(child_conn,))#將管道一方傳送到子進(jìn)程progress.start()print("主線程收:",parent_conn.recv())#接收數(shù)據(jù)parent_conn.send("hellow son")#發(fā)送數(shù)據(jù)progress.join()print("ending...")
  • The two connection objects returned by?Pipe()?represent the two ends of the pipe. Each connection object has?send()?and?recv()?methods (among others). Note that data in a pipe may become corrupted if two processes (or threads) try to read from or write to the?same?end of the pipe at the same time. Of course there is no risk of corruption from processes using different ends of the pipe at the same time

三:manager

Queue和pipe只是實現(xiàn)了數(shù)據(jù)交互,并沒實現(xiàn)數(shù)據(jù)共享,即一個進(jìn)程去更改另一個進(jìn)程的數(shù)據(jù)。

A manager object returned by?Manager()?controls a server process which holds Python objects and allows other processes to manipulate them using proxies.

A manager returned by?Manager()?will support types?list,?dict,?Namespace,?Lock,?RLock,?Semaphore,?BoundedSemaphore,?Condition,?Event,?Barrier,?Queue,?Value?and?Array. For example:

def foo(dict,list,i,string):dict["name"]="tom"list[0]=5list.append(i)string="i am tom"print("son process",id(dict),id(list)) if __name__=="__main__":with multiprocessing.Manager() as manager:dict=manager.dict({"name":"alex"})list=manager.list([1,2,3,4])string=manager.Value("i am alex")p_list=[]for i in range(2):progress=multiprocessing.Process(target=foo,args=(dict,list,i,string))p_list.append(progress)progress.start()for i in p_list:i.join()print(dict)print(list)print(string)print("father process", id(dict), id(list))

進(jìn)程池內(nèi)部維護(hù)一個進(jìn)程序列,當(dāng)使用時,則去進(jìn)程池中獲取一個進(jìn)程,如果進(jìn)程池序列中沒有可供使用的進(jìn)進(jìn)程,那么程序就會等待,直到進(jìn)程池中有可用進(jìn)程為止

進(jìn)程池中的方法:

  • apply 從進(jìn)程池里取一個進(jìn)程并同步執(zhí)行
  • apply_async 從進(jìn)程池里取出一個進(jìn)程并異步執(zhí)行
  • terminate 立刻關(guān)閉進(jìn)程池
  • join 主進(jìn)程等待所有子進(jìn)程執(zhí)行完畢,必須在close或terminete之后
  • close 等待所有進(jìn)程結(jié)束才關(guān)閉線程池
· 1 import multiprocessing,time,os 2 def foo(i): 3 time.sleep(1) 4 print(i,time.time()) 5 print("son",os.getpid())#17728 6 def success(arg): 7 print("success",os.getpid())#18340 8 def fail(arg): 9 print("falid") 10 if __name__ == '__main__': 11 print("main:",os.getpid())#18340 12 pool=multiprocessing.Pool() 13 lock=multiprocessing.Lock() 14 for i in range(20): 15 pool.apply_async(func=foo,args=(i,),callback=success,error_callback=fail) 16 #pool.apply(func=foo,args=(i,)) 17 pool.close() 18 pool.join() 19 print("ending....") 進(jìn)程池

?

所謂協(xié)程又稱為微線程,我們在進(jìn)程在創(chuàng)建時, 需要耗費時間和cpu資源;在創(chuàng)建多線程時,也需要消耗時間和資源。利用多協(xié)程的運行過程中,始終只要一個線程, 不存在創(chuàng)建線程和銷毀線程需要的時間; 也沒有線程切換的開銷, 任務(wù)需要開啟線程數(shù)越多, 協(xié)程的優(yōu)勢越明顯;更不需要多線程的鎖機制(GIL)。

協(xié)程擁有自己的寄存器上下文和棧。協(xié)程調(diào)度切換時,將寄存器上下文和棧保存到其他地方,在切回來的時候,恢復(fù)先前保存的寄存器上下文和棧。因此:協(xié)程能保留上一次調(diào)用時的狀態(tài)(即所有局部狀態(tài)的一個特定組合),每次過程重入時,就相當(dāng)于進(jìn)入上一次調(diào)用的狀態(tài),換種說法:進(jìn)入上一次離開時所處邏輯流的位置。

協(xié)程的優(yōu)點:

  (1)無需線程上下文切換的開銷,協(xié)程避免了無意義的調(diào)度,由此可以提高性能(但也因此,程序員必須自己承擔(dān)調(diào)度的責(zé)任,同時,協(xié)程也失去了標(biāo)準(zhǔn)線程使用多CPU的能力)

  (2)無需原子操作鎖定及同步的開銷

  (3)方便切換控制流,簡化編程模型

  (4)高并發(fā)+高擴展性+低成本:一個CPU支持上萬的協(xié)程都不是問題。所以很適合用于高并發(fā)處理。

協(xié)程的缺點:

  (1)無法利用多核資源:協(xié)程的本質(zhì)是個單線程,它不能同時將 單個CPU 的多個核用上,協(xié)程需要和進(jìn)程配合才能運行在多CPU上.當(dāng)然我們?nèi)粘K帉懙慕^大部分應(yīng)用都沒有這個必要,除非是cpu密集型應(yīng)用。

  (2)進(jìn)行阻塞(Blocking)操作(如IO時)會阻塞掉整個程序

?

def consumer(name):print("我要吃包子")while True:baozi=yieldprint("%s eating %s 包子"%(name,baozi)) def producer(name):c1.__next__()c2.__next__()n=0while True:print("\033[32;1m[producer]\033[0m is making baozi %s and %s" % (n, n + 1))c1.send(n)c2.send(n+1)n+=2if __name__ == '__main__':c1=consumer("alex")c2=consumer("tom")p1=producer("cookie")

import greenlet def test1():print(12)g2.switch()print(34)g2.switch()print(96) def test2():print(56)g1.switch()print(78) g1=greenlet.greenlet(test1)#創(chuàng)建一個協(xié)程對象 g2=greenlet.greenlet(test2) g1.switch()#啟動協(xié)程/切換協(xié)程

gevnet是協(xié)程之間的自動切換

?

import gevent def func1():print(12)gevent.sleep(2)print(56)def func2():print(34)gevent.sleep(1)print(78) t1=gevent.spawn(func1)#創(chuàng)建一個線程對象 t2=gevent.spawn(func2) print(t1) gevent.joinall([t1,t2])#添加線程對象,并運行線程

  

?

?

  

轉(zhuǎn)載于:https://www.cnblogs.com/Mr-l/p/10373862.html

總結(jié)

以上是生活随笔為你收集整理的python第10天(上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。