锁实例不能在两个 .py 文件之间共享
The lock instance cannot be shared between two .py files
最近接触多线程领域python。以下示例将更好地说明我的问题:
所以我有两个 .py 文件,一个是 test.py 另一个是 test2.py
在test.py中:
import time
from datetime import datetime
from threading import Thread, Lock
lock = Lock()
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(threadName)s %(name)s.%(funcName)s [%(levelname)s]: %(message)s')
def foo1():
while True:
with lock:
logging.info(datetime.now())
time.sleep(0.2)
if __name__ == '__main__':
from test2 import foo2
Thread(target=foo1).start()
Thread(target=foo2).start()
在test2.py中:
import time
from datetime import datetime
from test import logging, lock
def foo2():
while True:
with lock:
logging.info( datetime.now() )
time.sleep(5)
输出:
2017-03-17 17:11:06,210 Thread-1 root.foo1 [INFO]: 2017-03-17 17:11:06.210000
2017-03-17 17:11:06,210 Thread-2 root.foo2 [INFO]: 2017-03-17 17:11:06.210000
2017-03-17 17:11:06,415 Thread-1 root.foo1 [INFO]: 2017-03-17 17:11:06.416000
2017-03-17 17:11:06,619 Thread-1 root.foo1 [INFO]: 2017-03-17 17:11:06.620000
所以,锁好像没有作用。我测试了如果将 foo1 和 foo2 放在同一个 .py 文件中,它会起作用。
谁能告诉我为什么?
我知道这可能涉及到python如何处理导入的一些基础知识,请原谅我的无知。
这是 Python 导入系统中真正令人困惑的部分之一。您没有两个模块,test
和 test2
。您有三个模块,__main__
、test
和 test2
。同样,您没有一把锁 test.lock
。 你有两把锁,__main__.lock
和 test.lock
。
当您 运行 python test.py
时,Python 开始执行 test.py
作为 __main__
模块,而不是 test
模块。当 __main__
导入 test2
并且 test2
导入 test
时,Python 开始 运行ning test.py
again,这次作为 test
模块。不在 if __name__ == '__main__'
内的所有内容都会再次获得 运行,包括 lock = Lock()
,进行第二次锁定。
在 __main__
中,您将线程设置为 运行 的两个函数是 __main__.foo1
和 test2.foo2
。 __main__.foo1
使用 __main__.lock
,而 test2.foo2
使用从 test
导入的 test.lock
。由于这些是不同的锁,所以没有互斥。
与您的问题没有直接关系,但不要调用您的模块 test
,因为已经有一个同名的标准库模块,并且不要使用循环导入。 test
导入 test2
和 test2
导入 test
将导致各种讨厌的错误。
最近接触多线程领域python。以下示例将更好地说明我的问题: 所以我有两个 .py 文件,一个是 test.py 另一个是 test2.py
在test.py中:
import time
from datetime import datetime
from threading import Thread, Lock
lock = Lock()
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(threadName)s %(name)s.%(funcName)s [%(levelname)s]: %(message)s')
def foo1():
while True:
with lock:
logging.info(datetime.now())
time.sleep(0.2)
if __name__ == '__main__':
from test2 import foo2
Thread(target=foo1).start()
Thread(target=foo2).start()
在test2.py中:
import time
from datetime import datetime
from test import logging, lock
def foo2():
while True:
with lock:
logging.info( datetime.now() )
time.sleep(5)
输出:
2017-03-17 17:11:06,210 Thread-1 root.foo1 [INFO]: 2017-03-17 17:11:06.210000
2017-03-17 17:11:06,210 Thread-2 root.foo2 [INFO]: 2017-03-17 17:11:06.210000
2017-03-17 17:11:06,415 Thread-1 root.foo1 [INFO]: 2017-03-17 17:11:06.416000
2017-03-17 17:11:06,619 Thread-1 root.foo1 [INFO]: 2017-03-17 17:11:06.620000
所以,锁好像没有作用。我测试了如果将 foo1 和 foo2 放在同一个 .py 文件中,它会起作用。
谁能告诉我为什么?
我知道这可能涉及到python如何处理导入的一些基础知识,请原谅我的无知。
这是 Python 导入系统中真正令人困惑的部分之一。您没有两个模块,test
和 test2
。您有三个模块,__main__
、test
和 test2
。同样,您没有一把锁 test.lock
。 你有两把锁,__main__.lock
和 test.lock
。
当您 运行 python test.py
时,Python 开始执行 test.py
作为 __main__
模块,而不是 test
模块。当 __main__
导入 test2
并且 test2
导入 test
时,Python 开始 运行ning test.py
again,这次作为 test
模块。不在 if __name__ == '__main__'
内的所有内容都会再次获得 运行,包括 lock = Lock()
,进行第二次锁定。
在 __main__
中,您将线程设置为 运行 的两个函数是 __main__.foo1
和 test2.foo2
。 __main__.foo1
使用 __main__.lock
,而 test2.foo2
使用从 test
导入的 test.lock
。由于这些是不同的锁,所以没有互斥。
与您的问题没有直接关系,但不要调用您的模块 test
,因为已经有一个同名的标准库模块,并且不要使用循环导入。 test
导入 test2
和 test2
导入 test
将导致各种讨厌的错误。