欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python多進程同步Lock、Semaphore、Event實例

系統(tǒng) 1818 0

同步的方法基本與多線程相同。

1) Lock

當多個進程需要訪問共享資源的時候,Lock可以用來避免訪問的沖突。

復(fù)制代碼 代碼如下:

import multiprocessing
import sys

def worker_with(lock, f):
??? with lock:
??????? fs = open(f,"a+")
??????? fs.write('Lock acquired via with\n')
??????? fs.close()
???????
def worker_no_with(lock, f):
??? lock.acquire()
??? try:
??????? fs = open(f,"a+")
??????? fs.write('Lock acquired directly\n')
??????? fs.close()
??? finally:
??????? lock.release()

if __name__ == "__main__":

??? f = "file.txt"
?
??? lock = multiprocessing.Lock()
??? w = multiprocessing.Process(target=worker_with, args=(lock, f))
??? nw = multiprocessing.Process(target=worker_no_with, args=(lock, f))

??? w.start()
??? nw.start()

??? w.join()
??? nw.join()

在上面的例子中,如果兩個進程沒有使用lock來同步,則他們對同一個文件的寫操作可能會出現(xiàn)混亂。

2)Semaphore

Semaphore用來控制對共享資源的訪問數(shù)量,例如池的最大連接數(shù)。

復(fù)制代碼 代碼如下:

import multiprocessing
import time

def worker(s,i):
??? s.acquire()
??? print(multiprocessing.current_process().name + " acquire")
??? time.sleep(i)
??? print(multiprocessing.current_process().name + " release")
??? s.release()

if __name__ == "__main__":
?
??? s = multiprocessing.Semaphore(2)
??? for i in range(5):
??????? p = multiprocessing.Process(target=worker, args=(s,i*2))
??????? p.start()

上面的實例中使用semaphore限制了最多有2個進程同時執(zhí)行。

3)Event

Event用來實現(xiàn)進程間同步通信。

復(fù)制代碼 代碼如下:

import multiprocessing
import time

def wait_for_event(e):
??? """Wait for the event to be set before doing anything"""
??? print ('wait_for_event: starting')
??? e.wait()
??? print ('wait_for_event: e.is_set()->' + str(e.is_set()))

def wait_for_event_timeout(e, t):
??? """Wait t seconds and then timeout"""
??? print ('wait_for_event_timeout: starting')
??? e.wait(t)
??? print ('wait_for_event_timeout: e.is_set()->' + str(e.is_set()))


if __name__ == '__main__':
??? e = multiprocessing.Event()
??? w1 = multiprocessing.Process(name='block',
???????????????????????????????? target=wait_for_event,
???????????????????????????????? args=(e,))
??? w1.start()

??? w2 = multiprocessing.Process(name='non-block',
???????????????????????????????? target=wait_for_event_timeout,
???????????????????????????????? args=(e, 2))
??? w2.start()

??? time.sleep(3)
??? e.set()
??? print ('main: event is set')
???
#the output is:
#wait_for_event_timeout: starting
#wait_for_event: starting
#wait_for_event_timeout: e.is_set()->False
#main: event is set
#wait_for_event: e.is_set()->True


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本免费高清一区 | 91视频官网| 污免费网站 | 欧美国产日韩在线 | 国产精品视频免费的 | 在线日韩精品视频 | 高清中文字幕免费观在线 | 亚洲精品第一页 | 婷婷色综合久久 | 一级黄色免费毛片 | 久久最新精品 | 一区二区三区视频免费观看 | 天天操夜操 | 国产精品人妻无码久久久郑州 | 天天碰天天干 | 99在线精品免费视频九九视 | 2020天天狠天天透天干天天怕 | 国产精品高清在线观看 | a在线v | 欧美成人免费全网站大片 | 三级网址在线播放 | 免费一级毛片在线播放视频 | 天天干天天拍天天操 | 日本一道在线 | 国产91成人精品亚洲精品 | 亚洲精品a | 一级做a爱片特黄在线观看yy | 成人免费精品 | 亚洲国产欧美在线人网站 | 天天影视综合网 | 欧美成人免费高清二区三区 | 国产一级性生活视频 | 久久机热 | 日韩精品欧美一区二区三区 | 边摸边吃奶边做激情叫床文章 | 国产精品成人自拍 | 日韩一区中文字幕 | 特黄a级片 | 国产亚洲精品精品国产亚洲综合 | 国产精品网址你懂的 | 欧美性色综合网 |