您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用python线程锁和circulair导入时出现意外行为

5b51 2022/1/14 8:20:49 python 字数 4620 阅读 465 来源 www.jb51.cc/python

我用线程锁编写了一个简单的测试程序.此程序的行为不符合预期,python解释器不会抱怨.test1.py:from __future__ import with_statement from threading import Thread, RLock import time import test2 lock = RLock() class Test1

概述

我用线程锁编写了一个简单的测试程序.此程序的行为不符合预期,python解释器不会抱怨.

test1.py:

from __future__ import with_statement
from threading import Thread,RLock
import time
import test2

lock = RLock()

class Test1(object):
    def __init__(self):
        print("Start Test1")
        self.test2 = test2.Test2()
        self.__Thread = Thread(target=self.myThread,name="thread")
        self.__Thread.daemon = True
        self.__Thread.start()
        self.test1Method()

    def test1Method(self):
        print("start test1Method")
        with lock:
            print("entered test1Method")
            time.sleep(5)
            print("end test1Method")

    def myThread(self):
        self.test2.test2Method()

if __name__ == "__main__":
    client = Test1()
    raw_input()

test2.py:

from __future__ import with_statement
import time
import test1

lock = test1.lock

class Test2(object):
    def __init__(self):
        print("Start Test2")

    def test2Method(self):
        print("start test2Method")
        with lock:
            print("entered test2Method")
            time.sleep(5)
            print("end test2Method")

两个睡眠都在同一时间执行!不是我在使用锁时的预期.

当test2Method移动到test1.py时一切正常.当我在test2.py中创建锁并将其导入test1.py时,一切正常.当我在一个单独的源文件中创建锁并在test1.py和test2.py中导入它时一切正常.

可能它与circulair进口有关.

但为什么python不抱怨这个?

所以一个解决你的问题(远远不是最好的),看看发生了什么,你可以改变你的2脚本:

test1.py:

from __future__ import with_statement
from threading import Thread,RLock
import time

lock = RLock()

class Test1(object):
    def __init__(self):
        print("Start Test1")
        import test2    # <<<<<<<<<<<<<<<<<<<<<<<< Import is done here to be able to refer to __main__.lock.
        self.test2 = test2.Test2()
        self.__Thread = Thread(target=self.myThread,name="thread")
        self.__Thread.daemon = True
        self.__Thread.start()
        self.test1Method()

    def test1Method(self):
        print("start test1Method")
        with lock:
            print("entered test1Method")
            time.sleep(5)
            print("end test1Method")

    def myThread(self):
        self.test2.test2Method()

if __name__ == "__main__":
    client = Test1()
    raw_input()

test2.py:

from __future__ import with_statement
import time
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<< test1 is changed to __main__ to get the same lock as the one used in the launched script.
import __main__

lock = __main__.lock

class Test2(object):
    def __init__(self):
        print("Start Test2")

    def test2Method(self):
        print("start test2Method")
        with lock:
            print("entered test2Method")
            time.sleep(5)
            print("end test2Method")

HTH,

总结

以上是编程之家为你收集整理的使用python线程锁和circulair导入时出现意外行为全部内容,希望文章能够帮你解决使用python线程锁和circulair导入时出现意外行为所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶