Thanks, I think I get the answer. thanks all, have a good day!
从三星移动设备发送 -------- 原始邮件 -------- 发件人: MRAB <pyt...@mrabarnett.plus.com> 日期:2018-08-10 03:05 (GMT+08:00) 收件人: python-list@python.org 主题: Re: name 'aLOCK' is not defined When I add aLOCK = threading.RLock() behind if __name__ == "__main__" On 2018-08-09 16:16, xuanwu348 wrote: > Hi team > > Good day > The problem I meet when I add "aLOCK = threading.RLock()" to PositionB, the > program will report error "name 'aLOCK' is not defined ", > but when I change this code to PositionA, it can run normally, is there any > difference for the code between 'if __name__ == "__main__:"', can you help > me, thanks! > > The file I was attached, please change the extend ".pyx" to ".py", thanks, > code as below, tried python2.7 and python3.4: > > import threading > import time > from multiprocessing import Process > > #PositionA > aLOCK = threading.RLock() > > def Save_runnedCMD(filename, strings): > aLOCK.acquire() > with open(filename, "at") as f: > f.write(str(strings) + "\n\r") > aLOCK.release() > > def runCMD(filename): > time.sleep(1) > cmd = "testtest" > Save_runnedCMD(filename, cmd) > > > def Thr_run(filename): > t = [] > for i in range(2): > tt = threading.Thread(target = runCMD, args=(filename,)) > tt.start() > t.append(tt) > for tt in t: > tt.join() > > if __name__ == "__main__": > filename = "./testaaaaa.log" > > #PositionB > #aLOCK = threading.RLock() > > while 1: > t1 = Process(target=Thr_run, args=(filename, )) > t1.start() > t1.join() > > Error info as below: > D:\Appppp>python testa.py > Exception in thread Thread-1: > Traceback (most recent call last): > File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner > self.run() > File "C:\Python34\lib\threading.py", line 869, in run > self._target(*self._args, **self._kwargs) > File "D:\Appppp\testa.py", line 15, in runCMD > Save_runnedCMD(filename, cmd) > File "D:\Appppp\testa.py", line 7, in Save_runnedCMD > aLOCK.acquire() > NameError: name 'aLOCK' is not defined > > Exception in thread Thread-2: > Traceback (most recent call last): > File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner > self.run() > File "C:\Python34\lib\threading.py", line 869, in run > self._target(*self._args, **self._kwargs) > File "D:\Appppp\testa.py", line 15, in runCMD > Save_runnedCMD(filename, cmd) > File "D:\Appppp\testa.py", line 7, in Save_runnedCMD > aLOCK.acquire() > NameError: name 'aLOCK' is not defined > When you run the script, __name__ will be "__main__". When script uses the multiprocessing module to run the function "Thr_run" in another process, it actually starts another instance of itself, and in that other instance, name _won't_ be "__main__". Here's a simple example: from multiprocessing import Process def test_func(): print('test_func') print('__name__ is {!a}'.format(__name__)) if __name__ == "__main__": p = Process(target=test_func) p.start() p.join() When run, it prints: __name__ is '__main__' __name__ is '__mp_main__' test_func So, if you put the lock at PositionB, it won't be created by the subprocess. -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list