如何在类方法上调用线程并传递“self”?我有一个定义如下的类,想要在新线程中以 self 作为参数调用类方法。我尝试遵循,但 self 没有作为参数传递
cust_obj = Customer()
thread.start_new_thread(cust_obj.process, ())
class Customer():
def __init__(self):
pass
def process(self):
self.fetch_data()
self.serialize_data()
def fetch_data(self):
# Fetch data logic
pass
def serialize_data(self):
# Serialize fetched data
pass
最佳答案
我认为您应该将类定义放在实例创建之前。然后就可以工作了。
class Customer():
...
cust_obj = Customer()
thread.start_new_thread(cust_obj.process, ())
关于python - 在类方法上创建线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14082282/