python - 在 python 中,将 redis-py 与多处理模块一起使用,为什么每个进程都是不同的 fd?

标签 python linux python-2.7 redis redis-py

在 python 中,使用带有多处理模块的 redis-py,为什么每个进程都是不同的 fd?

测试代码:

    # xiaorui.cc

import time
import multiprocessing

import redis

r = redis.Redis(host='127.0.0.1', port=6379, db=0)

def func(msg):
    for i in xrange(30):
        time.sleep(1)
        print r.keys()
    return "done " + msg


if __name__ == "__main__":
    pool = multiprocessing.Pool(processes=4)
    result = []
    for i in xrange(4):
        msg = "hello %d" %(i)
        result.append(pool.apply_async(func, (msg, )))
    pool.close()
    pool.join()
    for res in result:
        print res.get()
    print "Sub-process(es) done."

测试结果:

[ruifengyun@xiaorui.cc ~]$ ps aux f|grep a.py
508      11704  5.0  0.0 421096 11664 pts/11   Sl+  17:50   0:00  |           \_ python a.py
508      11709  0.0  0.0 193760  7464 pts/11   S+   17:50   0:00  |               \_ python a.py
508      11710  0.0  0.0 193760  7468 pts/11   S+   17:50   0:00  |               \_ python a.py
508      11711  0.0  0.0 193760  7468 pts/11   S+   17:50   0:00  |               \_ python a.py
508      11712  0.0  0.0 193760  7476 pts/11   S+   17:50   0:00  |               \_ python a.py
508      11720  0.0  0.0 103248   832 pts/12   S+   17:50   0:00              \_ grep a.py
[ruifengyun@xiaorui.cc ~]$ sudo lsof -p 11709|grep 6379
python  11709 ruifengyun    4u  IPv4 4173927407      0t0        TCP localhost:51433->localhost:6379 (ESTABLISHED)
[ruifengyun@xiaorui.cc ~]$ sudo lsof -p 11710|grep 6379
python  11710 ruifengyun    4u  IPv4 4173927417      0t0        TCP localhost:51435->localhost:6379 (ESTABLISHED)
[ruifengyun@xiaorui.cc ~]$ sudo lsof -p 11711|grep 6379
python  11711 ruifengyun    4u  IPv4 4173927411      0t0        TCP localhost:51434->localhost:6379 (ESTABLISHED)
[ruifengyun@xiaorui.cc ~]$ sudo lsof -p 11712|grep 6379
python  11712 ruifengyun    4u  IPv4 4173927416      0t0        TCP localhost:51436->localhost:6379 (ESTABLISHED)

为什么每个进程的redis conn fd不一样?在我的认知中,只有在惰性模式下创建redis connect,才会出现不同的redis fd。

并且,所有子进程都是共享的 r 对象(redis conect fd)。

最佳答案

每个进程都需要单独连接到Redis。并且单个出站端口将仅由单个进程使用。所以每个进程都有自己的端口。

关于python - 在 python 中,将 redis-py 与多处理模块一起使用,为什么每个进程都是不同的 fd?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37251241/

相关文章:

python - 如何让 Python-Telegram Bot 在没有收到命令的情况下发送消息?

python - 层序的输入0与层: expected axis -1 of input shape to have value 784不兼容

linux - # vs $ 在文章中记录代码时

python - 计算二进制文件中的出现次数 Python 2X

python - 如何将 Django 函数设置为通过按钮 onclick 在后台运行?

python - Python 的 xml.etree.ElementTree 叶元素的意外 bool 行为

linux - 如何根据内容重命名文本文件?

linux - ubuntu 将多个 .tar.gz 文件解压到新目录

python - 尝试压缩一个句子然后将其上传到文件

python-2.7 - 帮助程序扫描超过 90000 个文档失败?