繁体   English   中英

python:多线程:self作为全局变量

[英]python : multithreading : self as global variable

我对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连接,我在sub1方法中用str(globalVar)替换了globalVar

工作原理如下:

>>> ================================ 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 ...

sub1 不是“子功能”。 Python 有“嵌套”函数,但这不是它的一个例子。 “sub1”有一个完全不同的范围,在你的类下看不到任何变量。

如果你想访问类实例(self)变量和方法,你应该将“self”作为参数传递给sub1,而不是self.name,它只是字符串“Thread-#”

def sub1(thread_instance):
    print thread_instance.name, thread_instance.threadID

然后在你的“run”方法中,用 self 调用它:

def run(self):
    sub1(self)

不要在线程中使用全局变量来存储来自线程的对象,因为当多个线程读取和写入此变量时,这肯定会引起头痛。

另外,如果线程 ID 和计数器参数是一个并且相同,那么它们有什么意义呢? 此外,您的代码示例不完整且缺少代码,因此很难理解您要通过此实现什么以及您尝试做什么。

另一件事,你不需要每行后面的分号,这不是 C。我会先专注于理解 Python 语言,然后在使用线程之前先了解一些基础知识。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM