日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python基于mysql實現(xiàn)的簡單隊列以及跨進程鎖實例詳解

系統(tǒng) 1744 0

通常在我們進行多進程應(yīng)用開發(fā)的過程中,不可避免的會遇到多個進程訪問同一個資源(臨界資源)的狀況,這時候必須通過加一個全局性的鎖,來實現(xiàn)資源的同步訪問(即:同一時間里只能有一個進程訪問資源)。

舉個例子如下:

假設(shè)我們用mysql來實現(xiàn)一個任務(wù)隊列,實現(xiàn)的過程如下:

1. 在Mysql中創(chuàng)建Job表,用于儲存隊列任務(wù) ,如下:

            
create table jobs(
  id auto_increment not null primary key,
  message text not null,
  job_status not null default 0
);


          

message 用來存儲任務(wù)信息,job_status用來標識任務(wù)狀態(tài),假設(shè)只有兩種狀態(tài),0:在隊列中, 1:已出隊列?
?
2. 有一個生產(chǎn)者進程,往job表中放新的數(shù)據(jù) ,進行排隊:

            
insert into jobs(message) values('msg1');

          

3.假設(shè)有多個消費者進程,從job表中取排隊信息 ,要做的操作如下:

            
select * from jobs where job_status=0 order by id asc limit 1;
update jobs set job_status=1 where id = ?; -- id為剛剛?cè)〉玫挠涗沬d

          

4. 如果沒有跨進程的鎖,兩個消費者進程有可能同時取到重復的消息,導致一個消息被消費多次。這種情況是我們不希望看到的,于是,我們需要實現(xiàn)一個跨進程的鎖。

=========================分割線=======================================

說到跨進程的鎖實現(xiàn),我們主要有幾種實現(xiàn)方式:

(1)信號量
(2)文件鎖fcntl
(3)socket(端口號綁定)
(4)signal
這幾種方式各有利弊,總體來說前2種方式可能多一點,這里我就不詳細說了,大家可以去查閱資料。
?
查資料的時候發(fā)現(xiàn)mysql中有鎖的實現(xiàn),適用于對于性能要求不是很高的應(yīng)用場景,大并發(fā)的分布式訪問可能會有瓶頸.
?
對此用python實現(xiàn)了一個demo,如下:
?
文件名:glock.py

            
#!/usr/bin/env python2.7 
# 
# -*- coding:utf-8 -*- 
# 
#  Desc  : 
# 
import logging, time 
import MySQLdb 
class Glock: 
  def __init__(self, db): 
    self.db = db 
  def _execute(self, sql): 
    cursor = self.db.cursor() 
    try: 
      ret = None 
      cursor.execute(sql) 
      if cursor.rowcount != 1: 
        logging.error("Multiple rows returned in mysql lock function.") 
        ret = None 
      else: 
        ret = cursor.fetchone() 
      cursor.close() 
      return ret 
    except Exception, ex: 
      logging.error("Execute sql \"%s\" failed! Exception: %s", sql, str(ex)) 
      cursor.close() 
      return None 
  def lock(self, lockstr, timeout): 
    sql = "SELECT GET_LOCK('%s', %s)" % (lockstr, timeout) 
    ret = self._execute(sql) 
 
    if ret[0] == 0: 
      logging.debug("Another client has previously locked '%s'.", lockstr) 
      return False 
    elif ret[0] == 1: 
      logging.debug("The lock '%s' was obtained successfully.", lockstr) 
      return True 
    else: 
      logging.error("Error occurred!") 
      return None 
  def unlock(self, lockstr): 
    sql = "SELECT RELEASE_LOCK('%s')" % (lockstr) 
    ret = self._execute(sql) 
    if ret[0] == 0: 
      logging.debug("The lock '%s' the lock is not released(the lock was not established by this thread).", lockstr) 
      return False 
    elif ret[0] == 1: 
      logging.debug("The lock '%s' the lock was released.", lockstr) 
      return True 
    else: 
      logging.error("The lock '%s' did not exist.", lockstr) 
      return None 
#Init logging 
def init_logging(): 
  sh = logging.StreamHandler() 
  logger = logging.getLogger() 
  logger.setLevel(logging.DEBUG) 
  formatter = logging.Formatter('%(asctime)s -%(module)s:%(filename)s-L%(lineno)d-%(levelname)s: %(message)s') 
  sh.setFormatter(formatter) 
  logger.addHandler(sh) 
  logging.info("Current log level is : %s",logging.getLevelName(logger.getEffectiveLevel())) 
def main(): 
  init_logging() 
  db = MySQLdb.connect(host='localhost', user='root', passwd='') 
  lock_name = 'queue' 
 
  l = Glock(db) 
 
  ret = l.lock(lock_name, 10) 
  if ret != True: 
    logging.error("Can't get lock! exit!") 
    quit() 
  time.sleep(10) 
  logging.info("You can do some synchronization work across processes!") 
  ##TODO 
  ## you can do something in here ## 
  l.unlock(lock_name) 
if __name__ == "__main__": 
  main() 


          

在main函數(shù)里:

l.lock(lock_name, 10) 中,10是表示timeout的時間是10秒,如果10秒還獲取不了鎖,就返回,執(zhí)行后面的操作。
?
在這個demo中,在標記TODO的地方,可以將消費者從job表中取消息的邏輯放在這里。即分割線以上的.

2.假設(shè)有多個消費者進程,從job表中取排隊信息,要做的操作如下:

            
select * from jobs where job_status=0 order by id asc limit 1;
update jobs set job_status=1 where id = ?; -- id為剛剛?cè)〉玫挠涗沬d

          

這樣,就能保證多個進程訪問臨界資源時同步進行了,保證數(shù)據(jù)的一致性。
?
測試的時候,啟動兩個glock.py, 結(jié)果如下:

            
[@tj-10-47 test]# ./glock.py  
2014-03-14 17:08:40,277 -glock:glock.py-L70-INFO: Current log level is : DEBUG 
2014-03-14 17:08:40,299 -glock:glock.py-L43-DEBUG: The lock 'queue' was obtained successfully. 
2014-03-14 17:08:50,299 -glock:glock.py-L81-INFO: You can do some synchronization work across processes! 
2014-03-14 17:08:50,299 -glock:glock.py-L56-DEBUG: The lock 'queue' the lock was released. 


          

可以看到第一個glock.py是 17:08:50解鎖的,下面的glock.py是在17:08:50獲取鎖的,可以證實這樣是完全可行的。

            
[@tj-10-47 test]# ./glock.py 
2014-03-14 17:08:46,873 -glock:glock.py-L70-INFO: Current log level is : DEBUG
2014-03-14 17:08:50,299 -glock:glock.py-L43-DEBUG: The lock 'queue' was obtained successfully.
2014-03-14 17:09:00,299 -glock:glock.py-L81-INFO: You can do some synchronization work across processes!
2014-03-14 17:09:00,300 -glock:glock.py-L56-DEBUG: The lock 'queue' the lock was released.
[@tj-10-47 test]#

          


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 英德市| 米易县| 彝良县| 绥滨县| 秭归县| 光泽县| 子洲县| 当雄县| 扶绥县| 榕江县| 安远县| 苏尼特左旗| 民和| 内乡县| 云龙县| 青神县| 同德县| 延安市| 璧山县| 东安县| 兴安县| 深圳市| 洛宁县| 延川县| 古田县| 望奎县| 上犹县| 河北省| 双桥区| 奉新县| 扎鲁特旗| 金川县| 四平市| 本溪| 临夏市| 蒲城县| 达州市| 鸡东县| 武威市| 靖边县| 东乌|