理解redis ConnectionPool中的_checkpid方法

def _checkpid(self):
    if self.pid != os.getpid():
        with self._check_lock:
            if self.pid == os.getpid():
                # another thread already did the work while we waited
                # on the lock.
                return
            self.disconnect()
            self.reset()

Problem:
when it get connection from pool or release to pool, it will both check pid, i don't understand why. if it run in multiprocess, it does not need the lock and it will have many identical pool. and if it run in multi thread, it will always get the same pid. any help is approciate.

Answer:
When a Unix process forks, it shares all sockets already opened in it with its children.

So if you create a connection pool, then make a request, and then fork, you will have a problem: While pools themselves get copied between the new processes, the sockets in the pool do not get copied and are shared across processes.

This can lead to a situation where one process writes to a redis client, and another waiting on the reply will get the wrong reply back.

However, newly created sockets AFTER the fork, will not be shared between the parent and children.

So by checking the pid, the pool checks if a fork has been made, and if so, resets all its sockets and creates new ones. This prevents very bad things from happening :)

PS:
So much thanks for your help! But i can not think out a situation where forks will happen, why do we need to fork, and in my opinion the checkpid method can only prevent what you said, could you give me a example?

python cannot utilize more than one CPU because of the GIL. Forking allows python programs to utilize multi-core machines. check out the Multiprocessing module in python for more details.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末抖韩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子犀呼,更是在濱河造成了極大的恐慌刊苍,老刑警劉巖遮晚,帶你破解...
    沈念sama閱讀 211,884評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡琉闪,警方通過查閱死者的電腦和手機(jī)邦泄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,347評論 3 385
  • 文/潘曉璐 我一進(jìn)店門删窒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人顺囊,你說我怎么就攤上這事肌索。” “怎么了特碳?”我有些...
    開封第一講書人閱讀 157,435評論 0 348
  • 文/不壞的土叔 我叫張陵诚亚,是天一觀的道長。 經(jīng)常有香客問我午乓,道長站宗,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,509評論 1 284
  • 正文 為了忘掉前任益愈,我火速辦了婚禮梢灭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己敏释,他們只是感情好库快,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,611評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著颂暇,像睡著了一般缺谴。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上耳鸯,一...
    開封第一講書人閱讀 49,837評論 1 290
  • 那天湿蛔,我揣著相機(jī)與錄音,去河邊找鬼县爬。 笑死阳啥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的财喳。 我是一名探鬼主播察迟,決...
    沈念sama閱讀 38,987評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼耳高!你這毒婦竟也來了扎瓶?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,730評論 0 267
  • 序言:老撾萬榮一對情侶失蹤泌枪,失蹤者是張志新(化名)和其女友劉穎概荷,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體碌燕,經(jīng)...
    沈念sama閱讀 44,194評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡误证,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,525評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了修壕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片愈捅。...
    茶點(diǎn)故事閱讀 38,664評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖慈鸠,靈堂內(nèi)的尸體忽然破棺而出蓝谨,到底是詐尸還是另有隱情,我是刑警寧澤青团,帶...
    沈念sama閱讀 34,334評論 4 330
  • 正文 年R本政府宣布像棘,位于F島的核電站,受9級特大地震影響壶冒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜截歉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,944評論 3 313
  • 文/蒙蒙 一胖腾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦咸作、人聲如沸锨阿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,764評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽墅诡。三九已至,卻和暖如春桐智,著一層夾襖步出監(jiān)牢的瞬間末早,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,997評論 1 266
  • 我被黑心中介騙來泰國打工说庭, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留然磷,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,389評論 2 360
  • 正文 我出身青樓刊驴,卻偏偏與公主長得像姿搜,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子捆憎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,554評論 2 349

推薦閱讀更多精彩內(nèi)容