我是Python新手。我試圖用嚴格的閱讀器優先級來編寫讀者 - 作者問題,出於某種原因,python不考慮我的全局變量'nor'。線程定義是否存在問題?Python 2.7中的線程
import threading
readers=threading.BoundedSemaphore(1)
writers=threading.BoundedSemaphore(1)
mutex=threading.BoundedSemaphore(1)
nor=0
class reader(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while(1):
writers.acquire()
mutex.acquire()
if(nor==0):
readers.acquire()
nor=nor+1
mutex.release()
print "I just read\n"
mutex.acquire()
if(nor==1):
readers.release()
nor=nor-1
mutex.release()
writers.release()
class writer(threading.Thread):
def _init__(self):
threading.Thread.__init__(self)
def run(self):
while(1):
writers.acquire()
readers.acquire()
print "I just wrote\n"
writers.release()
readers.release()
r1=reader()
r2=reader()
r3=reader()
w1=writer()
w2=writer()
r1.start()
r2.start()
r3.start()
w1.start()
w2.start()
Python 17?你的意思是Python 2.7嗎? –
是的,錯字。壞的一個。 – Shehzaad