Skip to main content
 首页 » 编程设计

python : multithreading : self as global variable

2024年10月24日39cloudgamer

我在 python 的多线程方面遇到了一些问题: 生成一个线程,它会得到一些参数,如线程名称、计数器等。在线程的“运行”部分,它正在调用一些子函数(并且还有一些子函数达到一定深度)但是,自变量(类)似乎不存在于子函数中:指的是 self.name显示一些错误(NameError: global name 'self' is not defined ).有没有什么方法可以在没有 (!!) 参数化所有内容的情况下获取这些子函数中完整结构的内容(这将在深度 4 时变得该死的长......)。希望这个简短的例子能更好地解释它,在 sub1 中,第二行打印尝试访问 self.counter

#!/usr/bin/python 
 
import threading 
import time 
 
globalVar = 1; 
 
def sub1  ( name ): 
  global globalVar ; 
 
  print str(globalVar) + " in der 1. subfunktion von " +str(name) 
  print "teste self"  + str(self.counter) + " - " + globalVar + " " +str(name) ; 
  globalVar += 1 ; 
  time.sleep(1); 
  sub2 (name) ; 
  return None ; 
 
 
class myThread (threading.Thread): 
    def __init__(self, threadID, name, counter): 
        threading.Thread.__init__(self) 
        self.threadID = threadID 
        self.name = name 
        self.counter = counter 
    def run(self): 
        print "Starting " + self.name + " mit zaehler " + str(self.counter) 
        sub1 (self.name); 
 
threadLock = threading.Lock() 
threads = [] ; 
 
# Create new threads 
count =0; 
while count < 10 : 
        count += 1; 
        threadX = myThread(count, "Thread-" + str(count), count) 
        threadX.start() 
        threads.append(threadX) 
 
for t in threads: 
    t.join() 
print "Exiting Main Thread" 

谢谢你的帮助

请您参考如下方法:

为什么你不直接通过 self而不是 name ?

#!/usr/bin/python 
 
import threading 
import time 
 
globalVar = 1; 
 
def sub1  ( self ): 
  global globalVar ; 
 
  print str(globalVar) + " in der 1. subfunktion von " +str(self.name) 
  print "teste self"  + str(self.counter) + " - " + str(globalVar) + " " +str(self.name) ; 
  globalVar += 1 ; 
  time.sleep(1); 
  sub2 (self.name) ; 
  return None ; 
 
 
class myThread (threading.Thread): 
    def __init__(self, threadID, name, counter): 
        threading.Thread.__init__(self) 
        self.threadID = threadID 
        self.name = name 
        self.counter = counter 
    def run(self): 
        print "Starting " + self.name + " mit zaehler " + str(self.counter) 
        sub1 (self); 
 
threadLock = threading.Lock() 
threads = [] ; 
 
# Create new threads 
count =0; 
while count < 10 : 
        count += 1; 
        threadX = myThread(count, "Thread-" + str(count), count) 
        threadX.start() 
        threads.append(threadX) 
 
for t in threads: 
    t.join() 
print "Exiting Main Thread" 

由于 int无法与 string 连接, 我替换了 globalVar与 str(globalVar)在sub1方法。

工作如下:

>>> ================================ RESTART ================================ 
>>>  
Starting Thread-1 mit zaehler 1 
1 in der 1. subfunktion von Thread-1 
teste self1 - 1 Thread-1 
Starting Thread-2 mit zaehler 2 
2 in der 1. subfunktion von Thread-2 
teste self2 - 2 Thread-2Starting Thread-3 mit zaehler 3 
 Starting Thread-5 mit zaehler 5 
Starting Thread-6 mit zaehler 6Starting Thread-7 mit zaehler 7Starting Thread-4 mit zaehler 4Starting Thread-8 mit zaehler 8Starting Thread-9 mit zaehler 9 
 3 in der 1. subfunktion von Thread-3 
 
 
 
 
3 in der 1. subfunktion von Thread-5Starting Thread-10 mit zaehler 10 
3 in der 1. subfunktion von Thread-63 in der 1. subfunktion von Thread-73 in der 1. subfunktion von Thread-9 
teste self3 - 3 Thread-3 
3 in der 1. subfunktion von Thread-43 in der 1. subfunktion von Thread-8 
 
 
teste self5 - 3 Thread-5 
teste self7 - 3 Thread-7 
 
3 in der 1. subfunktion von Thread-10teste self6 - 3 Thread-6teste self9 - 3 Thread-9 
teste self4 - 4 Thread-4 
teste self8 - 5 Thread-8 
 
teste self10 - 6 Thread-10 
 
 
//+ A bunch of Sub2 is not defined errors ...