V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Go1denwind
V2EX  ›  Python

Python 读写共享内存有什么优化方案没有

  •  
  •   Go1denwind · 2020-11-27 16:19:20 +08:00 · 1911 次点击
    这是一个创建于 1239 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现在的场景是
    多个进程 /线程去 sql 查询 然后将查询的结果写共享内存给到另一个代码进程(称为主进程吧)
    现在问题出在
    各个线程写给主进程性能堪忧
    8 条回复    2020-11-28 00:00:33 +08:00
    Go1denwind
        1
    Go1denwind  
    OP
       2020-11-27 16:26:06 +08:00
    如果线程的代码用 C 写 或者协程这类替换线程的方式 会不会有什么明显的提升
    能定位问题是在主线程整合各个线程的数据上耗时比较多
    NaVient
        2
    NaVient  
       2020-11-27 16:31:06 +08:00
    换 golang
    hasdream
        3
    hasdream  
       2020-11-27 16:36:25 +08:00
    查询 SQL,不涉及密集计算多线程就可以, 如果是根据查询的结果进行计算,那多进程 mutiprocessing 这个模块有个 Queue 可以进行进程通信 只能传输基本类型
    Go1denwind
        4
    Go1denwind  
    OP
       2020-11-27 16:47:45 +08:00
    @hasdream 哦补充下 查出来的结果要进行一些计算的
    multiprocessing.Queue 好像不支持不同进程间通讯 而且估计速度也和这样差不多
    Go1denwind
        5
    Go1denwind  
    OP
       2020-11-27 16:48:23 +08:00
    @NaVient 转行了转行了 /cry
    laminux29
        6
    laminux29  
       2020-11-27 17:10:12 +08:00
    @Go1denwind 不看文档嘛?

    人家 Queue 就是用来做多线程同步的。

    你这种需求,正确的操作是,多线程获取数据后,通过 Queue 发送给主线程。不然你主线程还要等每个子线程写数据,这种操作方式本质是串行化,没办法完全利用硬件,速度当然慢。

    https://docs.python.org/3/library/queue.html
    hasdream
        7
    hasdream  
       2020-11-27 17:16:02 +08:00
    @Go1denwind multiprocessing.Queue 基于 multiprocessing.Process 创建的进程通信是没问题的。 如果你觉得不靠谱 那就 fork 多个进程 主进程创建多个 PIPE 进行通信也行
    jones2000
        8
    jones2000  
       2020-11-28 00:00:33 +08:00
    写共享内存和读取应该很快的, 每个线程或进程写单独的一块内存地址, 写完发送事件消息给主进程, 主进程就读对应地址的内存块。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2831 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:18 · PVG 14:18 · LAX 23:18 · JFK 02:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.