如何在 class 中 运行 线程函数?
How to run a threading function within a class?
我正在尝试 运行 我的简单 class 中的简单线程函数。
我正在尝试在 class 的方法中调用 Thread 函数。此方法内的 Thread 函数指向 class 内的另一个方法。我测试它的方法是通过 python 终端。这是我在 increment_thread.py 中的 class:
from threading import Thread
import time
class Increment:
def __init__(self):
self.count = 0
def add_one(self):
while True:
self.count = self.count + 1
time.sleep(5)
def start(self):
background_thread = Thread(target=add_one)
background_thread.start()
print("Started counting up")
return
def get_count(self):
return print(self.count)
为了对此进行测试,我在我的终端中 运行 python,这会提示 python 终端。
然后,我运行下面几行:
from increment_thread import Increment
inc = Increment()
inc.get_count() # Yields 0
inc.start()
我希望线程启动并指示 "Started counting up",但我却收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/python-sandbox/increment_thread.py", line 14, in start
background_thread = Thread(target=add_one)
NameError: name 'add_one' is not defined
我想做的事情可行吗?
在Thread构造函数中不应该是target=self.add_one而不是target=add_one
传递参数:
from threading import Thread
import time
class Increment:
count = None
def __init__(self):
self.count = 0
def add_one(self, start_at=0):
self.count = start_at
while True:
self.count = self.count + 1
time.sleep(5)
def start_inc(self, start_at=count):
# Pass args parameter as a tuple
background_thread = Thread(target=self.add_one, args=(start_at,))
background_thread.start()
print("Started counting up")
return
def get_count(self):
return print(self.count)
if __name__ == "__main__":
inc = Increment()
inc.get_count() # Yields 0
inc.start_inc(start_at=5)
while True:
inc.get_count()
time.sleep(2)
就像class字段一样,class方法需要使用self.method
语法来引用。所以
def start(self):
background_thread = Thread(target=self.add_one)
background_thread.start()
print("Started counting up")
return
我正在尝试 运行 我的简单 class 中的简单线程函数。
我正在尝试在 class 的方法中调用 Thread 函数。此方法内的 Thread 函数指向 class 内的另一个方法。我测试它的方法是通过 python 终端。这是我在 increment_thread.py 中的 class:
from threading import Thread
import time
class Increment:
def __init__(self):
self.count = 0
def add_one(self):
while True:
self.count = self.count + 1
time.sleep(5)
def start(self):
background_thread = Thread(target=add_one)
background_thread.start()
print("Started counting up")
return
def get_count(self):
return print(self.count)
为了对此进行测试,我在我的终端中 运行 python,这会提示 python 终端。
然后,我运行下面几行:
from increment_thread import Increment
inc = Increment()
inc.get_count() # Yields 0
inc.start()
我希望线程启动并指示 "Started counting up",但我却收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/python-sandbox/increment_thread.py", line 14, in start
background_thread = Thread(target=add_one)
NameError: name 'add_one' is not defined
我想做的事情可行吗?
在Thread构造函数中不应该是target=self.add_one而不是target=add_one
传递参数:
from threading import Thread
import time
class Increment:
count = None
def __init__(self):
self.count = 0
def add_one(self, start_at=0):
self.count = start_at
while True:
self.count = self.count + 1
time.sleep(5)
def start_inc(self, start_at=count):
# Pass args parameter as a tuple
background_thread = Thread(target=self.add_one, args=(start_at,))
background_thread.start()
print("Started counting up")
return
def get_count(self):
return print(self.count)
if __name__ == "__main__":
inc = Increment()
inc.get_count() # Yields 0
inc.start_inc(start_at=5)
while True:
inc.get_count()
time.sleep(2)
就像class字段一样,class方法需要使用self.method
语法来引用。所以
def start(self):
background_thread = Thread(target=self.add_one)
background_thread.start()
print("Started counting up")
return