I'm trying to learn about threading on Python 3. I made an example code:
import time
import threading
def myfunction(string,sleeptime,lock,*args):
count = 0
while count < 2:
#entering critical section
lock.acquire()
print(string, " Now sleeping after Lock acquired for ",sleeptime)
time.sleep(sleeptime)
print(string, " Now releasing lock and sleeping again.\n",time.ctime(time.time()))
lock.release()
#exiting critical section
time.sleep(sleeptime)
count+=1
#threading.Thread.daemon=True
if __name__!="__main__":
lock = threading.Lock()
try:
threading.Thread.start(myfunction("Thread Nº 1",2,lock))
threading.Thread.start(myfunction("Thread Nº 2",2,lock))
except:
raise
while 1:pass
It works in part. When it reaches the while<2 loop, it returns the error:
Traceback (most recent call last):
File "python", line 22, in <module>
AttributeError: 'NoneType' object has no attribute '_initialized'
And never executes the second thread call.
What can I do to correct this?
Thank you all!
__name__ != "__main__"doesn't look correct at all. Usually the condition is reversed...__name__ == '__main__'