python - 在类方法上创建线程

标签 python multithreading oop

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

相关文章:

c# - 当 ThreadPool 中的事件线程数大于 ThreadPool.GetMinThreads() 时启动任务

java - 外部类可以定义为静态并包含内部静态类吗?

oop - 为什么 Rust 不支持 trait 对象向上转换?

python - 当我的 python ddos​​ 脚本运行时出现错误

python - 在python中从redis排序字典列表

python - QSplitter() 不平等对待 QScrollArea() 和 QFrame()

python - 我试图理解类和函数,但似乎无法弄清楚我的代码出了什么问题

python - 编写全局配置文件的正确方法是什么?

c# - 关于 OoOP 的 Thread.MemoryBarrier() 错误的解释

c# - 这是对多线程设计的正确使用吗? (C#)