发布时间:2019-08-27 08:02:31编辑:auto阅读(2018)
'''自定义线程类'''
from threading import Thread
import time
#创建一个类,并继承Python的Thread类,且重写run()方法实现具体的执行顺序由自己来定义
class MyThread(Thread):
'''time.sleep代表等待10秒后才执行'''
def run(self):
time.sleep(2)
print("我是线程类...")
'''下面这一段是从源码Thread类中的run方法复制过来的,当然
没有也许,因为继承了Thread,就会去父类里面找,我这里只是测试用'''
try:
if self._target:
self._target(*self._args, **self._kwargs)
finally:
# Avoid a refcycle if the thread is running a function with
# an argument that has a member that points to the thread.
del self._target, self._args, self._kwargs
#定义一个函数
def Bar():
print("Bar...")
#通过自定义的线程类来创建线程,并且关联函数Bar,最后执行这个线程
t1 = MyThread(target=Bar)
t1.start()
print("守护线程执行结束...")
上一篇: python学习------指定字符串补
下一篇: Python中的循环退出举例及while
51920
51679
42008
38846
33332
30309
28949
23958
23876
22244
344°
2568°
3241°
2698°
2676°
3407°
2643°
3476°
5706°
5509°