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

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

概述我用线程锁编写了一个简单的测试程序.此程序的行为不符合预期,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_statementfrom threading import Thread,RLockimport timeimport test2lock = 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_statementimport timeimport test1lock = test1.lockclass 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不抱怨这个?

最佳答案在Python中使用$python test1.py执行python脚本时会发生什么情况,你的test1.py将作为__main__而不是test1导入,所以如果你想获得在启动脚本中定义的锁定,你就不应该导入test1但是你应该导入__main__,因为如果你做第一个,你将创建另一个与__main __.lock(test1.lock!= __main __ .lock)不同的锁.

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

test1.py:

from __future__ import with_statementfrom threading import Thread,RLockimport timelock = 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_statementimport time# <<<<<<<<<<<<<<<<<<<<<<<<<<<<< test1 is changed to __main__ to get the same lock as the one used in the launched script.import __main__lock = __main__.lockclass 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导入时出现意外行为所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1205434.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存