Python 2.7中的线程

Python 2.7中的线程,python,multithreading,global-variables,Python,Multithreading,Global Variables,我是Python新手。我试图用一个SradReader优先权来解决读者编写者的问题,因为某些原因,Python不考虑我的全局变量“NOR”。线程定义有问题吗 import threading readers=threading.BoundedSemaphore(1) writers=threading.BoundedSemaphore(1) mutex=threading.BoundedSemaphore(1) nor=0 class reader(threading.Thread):

我是Python新手。我试图用一个SradReader优先权来解决读者编写者的问题,因为某些原因,Python不考虑我的全局变量“NOR”。线程定义有问题吗

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()
试试提琴

global nor
在你的班里,在nor呼叫之前。之后

def run(self): 

解释。赋值应用于局部作用域,除非您向Python提供了一个指令,说明您正在处理全局作用域。就是这样!谢谢你的解释。Python 17?你是说Python 2.7吗?是的,打字错误。一个坏的。