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

如何确定某个函数或者某个变量正在被多个线程使用呢?

  •  
  •   quietin · 2015-12-31 18:08:33 +08:00 · 2956 次点击
    这是一个创建于 3031 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 python 中如何确定某个函数或者某个变量正在被多个线程(或进程)使用呢?
    举例场景,像sqlalchemy里用多线程共享一个数据库连接提交就会提示你正在用多个线程,提交不被允许。这是如何实现的呢?

    反过来,还想问一下,如何让一个变量(或函数)变成线程或进程独占的呢?
    比如在线程中,是不是用 thread local 就可以了, 比如这样,在一线程中独占变量a

    import local
    dummy = local()
    dummy.number = a
    del a

    但这好像得保证当前线程最先执行才可以

    希望能和大家交流讨论下,欢迎给出想法建议:)

    12 条回复    2016-01-03 11:14:24 +08:00
    yangtukun1412
        1
    yangtukun1412  
       2015-12-31 20:00:16 +08:00
    用 threading.RLock 应该可以实现
    quietin
        2
    quietin  
    OP
       2015-12-31 20:55:08 +08:00
    第一个问题谁能给个解答。。。
    sincway
        3
    sincway  
       2015-12-31 22:55:36 +08:00 via Android
    我觉得信号量可以实现
    quietin
        4
    quietin  
    OP
       2015-12-31 23:03:42 +08:00
    @sincway 可否稍微具体一些呢
    sincway
        5
    sincway  
       2015-12-31 23:46:13 +08:00 via Android   ❤️ 1
    @quietin 搜索 Python semaphore 即可。信号量可以保证只允许指定数目的进程或者线程访问某个资源,超过数量就会被阻塞。如果设置为 1 就是只允许一个同时访问。
    gamexg
        6
    gamexg  
       2016-01-01 00:14:18 +08:00 via Android
    内部保存当前操作线程即可,别忘了锁
    gamexg
        7
    gamexg  
       2016-01-01 00:17:15 +08:00 via Android   ❤️ 1
    对了,可以写一个修饰器,对所有公开函数加可重入锁。
    可能有这种库了。
    boyhailong
        8
    boyhailong  
       2016-01-01 19:00:59 +08:00   ❤️ 1
    貌似大家都在回答第二个问题,它的确不难
    至于第一个 你指的是运行期判断是不是有多线程调用吗 这个可以在函数或者变量前加个线程 id 的记录 如果有多于两个不同的线程 id 就说明在发生多线程调用啦 其实也很简单。
    quietin
        9
    quietin  
    OP
       2016-01-01 21:19:15 +08:00
    @boyhailong 谢谢回复。是的,可以用 threading.get_current().ident 得到线程标识
    quietin
        10
    quietin  
    OP
       2016-01-01 22:10:10 +08:00
    @boyhailong 我发现了一个问题,标识必须得么线程本身,不能用 ident ,并发四线程的时候
    <Thread(Thread-1, started 123145306509312)>
    <Thread(Thread-2, started 123145306509312)>
    <Thread(Thread-3, started 123145306509312)>
    <Thread(Thread-4, started 123145306509312)>
    它们的 ident 都是一样的。。。
    boyhailong
        11
    boyhailong  
       2016-01-02 19:35:59 +08:00   ❤️ 1
    @quietin python 线程是可以设置 name 的 最简单的就是启动线程的时候 设置 运行期获取就可以
    具体可以参考: http://www.tutorialspoint.com/python/python_multithreading.html

    In addition to the methods, the threading module has the Thread class that implements threading. The methods provided by the Thread class are as follows:

    run(): The run() method is the entry point for a thread.

    start(): The start() method starts a thread by calling the run method.

    join([time]): The join() waits for threads to terminate.

    isAlive(): The isAlive() method checks whether a thread is still executing.

    getName(): The getName() method returns the name of a thread.

    setName(): The setName() method sets the name of a thread.
    quietin
        12
    quietin  
    OP
       2016-01-03 11:14:24 +08:00
    @boyhailong 可以是可以,但是手动设置 name 感觉不是很好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4579 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 01:08 · PVG 09:08 · LAX 18:08 · JFK 21:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.