On Thu, Aug 09, 2018 at 11:16:37PM +0800, xuanwu348 wrote: > The problem I meet when I add "aLOCK = threading.RLock()" to PositionB, the > program will report error "name 'aLOCK' is not defined ",
You mean to say: *move*, not *add*. That is why aLock is out of scope. ..../python3-doc/html/tutorial/classes.html#python-scopes-and-namespaces > 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 > -- > https://mail.python.org/mailman/listinfo/python-list -- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B -- https://mail.python.org/mailman/listinfo/python-list