发布时间:2019-08-27 08:02:31编辑:auto阅读(1942)
'''自定义线程类'''
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
51264
50704
41302
38121
32581
29488
28344
23205
23176
21501
1575°
2295°
1903°
1845°
2155°
1887°
2575°
4316°
4162°
2971°