转发自https://blog.youkuaiyun.com/kelvinlll/article/details/70755389
# coding:utf-8
import time
from threading import Thread
def foo(number):
time.sleep(20)
return number
class MyThread(Thread):
def __init__(self, number):
Thread.__init__(self)
self.number = number
def run(self):
self.result = foo(self.number)
def get_result(self):
return self.result
thd1 = MyThread(3)
thd2 = MyThread(5)
thd1.start()
thd2.start()
thd1.join()
thd2.join()
print thd1.get_result()
print thd2.get_result()
本文展示了一个使用Python的threading模块实现多线程的示例。通过定义一个继承自Thread类的MyThread子类,并在其中实现run方法,使得线程能够执行特定的任务并返回结果。实例中创建了两个线程,分别执行不同的任务并获取结果。
807

被折叠的 条评论
为什么被折叠?



