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
bestehen
V2EX  ›  Python

为啥有的多线程的时候不加锁呢?

  •  
  •   bestehen · 2018-08-23 11:15:14 +08:00 · 2040 次点击
    这是一个创建于 2077 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://blog.csdn.net/chivalrousli/article/details/77602863 这是一个多线程处理文件的 项目 就没有加锁实现的多线程,请问这是为啥呢?

    4 条回复    2018-08-27 16:51:04 +08:00
    smilekung
        1
    smilekung  
       2018-08-23 11:33:09 +08:00
    因为没有资源竞争吧 每个线程各自处理不同目录的文件
    615915524
        2
    615915524  
       2018-08-23 11:49:06 +08:00
    cpu 密集型操作和 IO 密集型操作。

    后者没有锁
    Qzier
        3
    Qzier  
       2018-08-25 21:23:33 +08:00 via iPhone
    因为各个线程没有什么关联
    U87
        4
    U87  
       2018-08-27 16:51:04 +08:00
    @615915524 这话好像不对吧,仅就 py 而言,多线程也只是针对 io 密集型的,cpu 密集型没有真正意义上的多线程(因为 python 全局锁 GIL 的存在),所以多线程加不加锁是要看多线程之间有没有可能抢资源导致数据异常等问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2205 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 15:50 · PVG 23:50 · LAX 08:50 · JFK 11:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.