99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python互斥鎖、加鎖、同步機(jī)制、異步通信知識總結(jié)
python互斥鎖、加鎖、同步機(jī)制、異步通信知識總結(jié)
2018-05-21
收藏

python互斥鎖、加鎖、同步機(jī)制、異步通信知識總結(jié)

某個(gè)線程要共享數(shù)據(jù)時(shí),先將其鎖定,此時(shí)資源的狀態(tài)為“鎖定”,其他線程不能更改;直到該線程釋放資源,將資源的狀態(tài)變成“非鎖定”,其他的線程才能再次鎖定該資源?;コ怄i保證了每次只有一個(gè)線程進(jìn)入寫入操作,從而保證了多線程情況下數(shù)據(jù)的正確性。
采用f_flag的方法效率低
創(chuàng)建鎖
mutex=threading.Lock()
鎖定
mutex.acquire([blocking])#里面可以加blocking(等待的時(shí)間)或者不加,不加就會(huì)一直等待(堵塞)
釋放
mutex.release()    
import threading
from threading import Thread
from threading import Lock
import time
 
thnum=0
#兩個(gè)線程都在搶著對這個(gè)鎖進(jìn)行上鎖,如果有一方成功上鎖,那么導(dǎo)致另外一方會(huì)堵塞(一直等待),到這個(gè)鎖被解開為之
class MyThread(threading.Thread):
  def run(self):
    mutex.acquire()
    for i in range(10000):
      global thnum
      thnum+=1  
    print(thnum)
    mutex.release()  
def test():
  global thnum
  mutex.acquire() #等待可以上鎖,通知而不是輪訓(xùn),沒有占用CPU
  for i in range(10000):
    thnum+=1
  print(thnum)
  mutex.release()#解鎖
mutex=Lock()
if __name__=='__main__':
  t=MyThread()
  t.start()
 
#創(chuàng)建一把互斥鎖,默認(rèn)是沒有上鎖的
 
thn=Thread(target=test)
thn.start()
 
'''''
10000
20000
'''

只要一上鎖,由多任務(wù)變?yōu)閱稳蝿?wù),相當(dāng)于只有一個(gè)線程在運(yùn)行。

下面的代碼相對上面加鎖的時(shí)間變短了    
import threading
from threading import Thread
from threading import Lock
import time
 
thnum=0
#兩個(gè)線程都在搶著對這個(gè)鎖進(jìn)行上鎖,如果有一方成功上鎖,那么導(dǎo)致另外一方會(huì)堵塞(一直等待),到這個(gè)鎖被解開為之
class MyThread(threading.Thread):
  def run(self):
    for i in range(10000):
      mutex.acquire()
      global thnum
      thnum+=1
      mutex.release()#釋放后,都開始搶,這樣上鎖的時(shí)間變短  
    print(thnum)
      
def test():
  global thnum
  for i in range(10000):
    mutex.acquire()
    thnum+=1
    mutex.release()#解鎖
  print(thnum)
mutex=Lock()
if __name__=='__main__':
  t=MyThread()
  t.start()
 
#創(chuàng)建一把互斥鎖,默認(rèn)是沒有上鎖的
 
thn=Thread(target=test)
thn.start()
 
'''''
10000
20000
'''

只有必須加鎖的地方才加鎖

同步:按照預(yù)定的先后順序執(zhí)行

一個(gè)運(yùn)行完后,釋放下一個(gè),下一個(gè)鎖定后運(yùn)行,再釋放下一個(gè),下一個(gè)鎖定后,運(yùn)行后釋放下一個(gè)..... 釋放第一個(gè)

異步:    
#異步的實(shí)現(xiàn)
from multiprocessing import Pool
import time
import os
 
#getpid()獲取當(dāng)前進(jìn)程的進(jìn)程號
#getppid()獲取當(dāng)前進(jìn)程的父進(jìn)程號
 
def test():#子進(jìn)程
  print("----進(jìn)程池中的進(jìn)程-----pid=%d,ppid=%d --"%(os.getpid(),os.getppid()))
  for i in range(3):
    print("-----%d----"%i)
    time.sleep(1)
  return "over" #子進(jìn)程執(zhí)行完后返回給操作系統(tǒng),返回給父進(jìn)程
 
def test2(args):
  print("-----callback func----pid=%d"%os.getpid())#主進(jìn)程調(diào)用test2
  print("------callback func---args=%s"%args)
 
def main():
  pool=Pool(3)
  pool.apply_async(func=test,callback=test2)#回調(diào)
  time.sleep(5)#收到func進(jìn)程結(jié)束后的信號后,執(zhí)行回調(diào)函數(shù)test2
 
  print("----主進(jìn)程-pid = %d"%os.getpid())
 
if __name__=="__main__":
  #main()
  pool=Pool(3)
  pool.apply_async(test,callback=test2)#回調(diào)
  time.sleep(5)#收到func進(jìn)程結(jié)束后的信號后,執(zhí)行回調(diào)函數(shù)test2
 
  print("----主進(jìn)程-pid = %d"%os.getpid())
 
'''''顯示結(jié)果不太正確,應(yīng)該先運(yùn)行test呀,再運(yùn)行test2
-----callback func----pid=7044
------callback func---args=over
----主進(jìn)程-pid = 7044
----進(jìn)程池中的進(jìn)程-----pid=3772,ppid=7044 --
-----0----
-----1----
-----2----
'''

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }