理解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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容