在类方法上创建线程

时间:2012-12-29 13:49:24

标签: python multithreading oop

如何在类方法上调用线程传递'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

1 个答案:

答案 0 :(得分:2)

我相信你应该在创建实例之前放置类定义。然后它会工作。

class Customer():
    ...
cust_obj = Customer()
thread.start_new_thread(cust_obj.process, ())