Python 如何对每个线程进行全局计数器增量?
我正在尝试创建一个python文件,该文件将在每次线程启动时将全局计数器增加5。我的目标是让4个线程将计数器增加5,最终结果是20。以下是我目前掌握的代码:Python 如何对每个线程进行全局计数器增量?,python,multithreading,Python,Multithreading,我正在尝试创建一个python文件,该文件将在每次线程启动时将全局计数器增加5。我的目标是让4个线程将计数器增加5,最终结果是20。以下是我目前掌握的代码: import threading import time global counter counter =0 class MyThread(threading.Thread): counter = counter def __init__(self, name, del
import threading
import time
global counter
counter =0
class MyThread(threading.Thread):
counter = counter
def __init__(self, name, delay, counter=0):
threading.Thread.__init__(self)
self.name = name
self.delay = delay
self.counter = counter
def run(self):
print('Starting thread %s...'% self.name)
self.thread_lock.acquire()
self.thread_lock.release()
print('Finished thread %s...'% self.name)
thread_lock = threading.Lock()
thread1 = MyThread('A', 0.1)
thread2 = MyThread('B', 0.1)
thread3 = MyThread('C', 0.1)
thread4 = MyThread('D', 0.1)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread1.join()
thread2.join()
thread3.join()
thread4.join()
您可以使用计数器类和(锁定)函数来增加值:
import threading
import time
lock = threading.Lock()
class Counter:
def __init__(self, initial_count):
self.value = initial_count
def add(counter, name, delay, value):
with lock:
msg = f"{name} is sleeping for {delay} seconds, before increasing {counter.value} by {value}"
print(msg)
time.sleep(x)
counter.value = counter.value + value
counter = Counter(0)
increasingValue = 5
_threads = []
for x in range(4):
th = threading.Thread(
target=add, args=(counter, f"thread_{x}", x, increasingValue)
)
_threads.append(th)
th.start()
[t.join() for t in _threads]
print('>', counter.value)
输出:
您可以使用计数器类和(锁定)函数来增加值:
import threading
import time
lock = threading.Lock()
class Counter:
def __init__(self, initial_count):
self.value = initial_count
def add(counter, name, delay, value):
with lock:
msg = f"{name} is sleeping for {delay} seconds, before increasing {counter.value} by {value}"
print(msg)
time.sleep(x)
counter.value = counter.value + value
counter = Counter(0)
increasingValue = 5
_threads = []
for x in range(4):
th = threading.Thread(
target=add, args=(counter, f"thread_{x}", x, increasingValue)
)
_threads.append(th)
th.start()
[t.join() for t in _threads]
print('>', counter.value)
输出:
程序中有许多
计数器变量
counter =0
在全局命名空间中创建一个
class MyThread(threading.Thread):
counter = counter
def __init__(self, name, delay, counter=0):
...
self.counter = counter
在类命名空间中创建另一个
class MyThread(threading.Thread):
counter = counter
def __init__(self, name, delay, counter=0):
...
self.counter = counter
在thread类的每个实例中创建一个。实际上你并没有增加它们中的任何一个
如果确实需要单个全局计数器,请在递增该计数器的方法中使用global
关键字global
告诉函数应该在全局命名空间中解析变量,而不是在本地函数命名空间中解析变量。在模块级完全不需要它,您不能单方面声明一个全局变量。您只能告诉特定函数如何处理变量
import threading
import time
counter =0
class MyThread(threading.Thread):
def __init__(self, name, delay, counter=0):
threading.Thread.__init__(self)
self.name = name
self.delay = delay
def run(self):
global counter
print('Starting thread %s...'% self.name)
self.thread_lock.acquire()
counter += 5
self.thread_lock.release()
print('Finished thread %s...'% self.name)
thread_lock = threading.Lock()
thread1 = MyThread('A', 0.1)
thread2 = MyThread('B', 0.1)
thread3 = MyThread('C', 0.1)
thread4 = MyThread('D', 0.1)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread1.join()
thread2.join()
thread3.join()
thread4.join()
print(counter)
程序中有许多计数器变量
counter =0
在全局命名空间中创建一个
class MyThread(threading.Thread):
counter = counter
def __init__(self, name, delay, counter=0):
...
self.counter = counter
在类命名空间中创建另一个
class MyThread(threading.Thread):
counter = counter
def __init__(self, name, delay, counter=0):
...
self.counter = counter
在thread类的每个实例中创建一个。实际上你并没有增加它们中的任何一个
如果确实需要单个全局计数器,请在递增该计数器的方法中使用global
关键字global
告诉函数应该在全局命名空间中解析变量,而不是在本地函数命名空间中解析变量。在模块级完全不需要它,您不能单方面声明一个全局变量。您只能告诉特定函数如何处理变量
import threading
import time
counter =0
class MyThread(threading.Thread):
def __init__(self, name, delay, counter=0):
threading.Thread.__init__(self)
self.name = name
self.delay = delay
def run(self):
global counter
print('Starting thread %s...'% self.name)
self.thread_lock.acquire()
counter += 5
self.thread_lock.release()
print('Finished thread %s...'% self.name)
thread_lock = threading.Lock()
thread1 = MyThread('A', 0.1)
thread2 = MyThread('B', 0.1)
thread3 = MyThread('C', 0.1)
thread4 = MyThread('D', 0.1)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread1.join()
thread2.join()
thread3.join()
thread4.join()
print(counter)
Python中的全局变量不是这样定义或检索的。请看一些实际使用的例子。只有在访问值时才必须使用global
关键字;要定义一个全局变量,除了在顶级作用域中声明一个变量外,您不需要做任何特殊的事情。因此,在您的代码中,将全局计数器
替换为计数器=0
,并在开始处将全局计数器
添加到\uuuu init\uuu
函数中。这解决了问题吗?Python中的全局变量不是这样定义或检索的。请看一些实际使用的例子。只有在访问值时才必须使用global
关键字;要定义一个全局变量,除了在顶级作用域中声明一个变量外,您不需要做任何特殊的事情。因此,在您的代码中,将全局计数器
替换为计数器=0
,并在开始处将全局计数器
添加到\uuuu init\uuu
函数中。这能解决问题吗?