python学习笔记——线程threading (二)重写run()方法和守护进程daemon()

本文介绍了Python线程的`run()`方法重写及其在单个和多个线程中的应用,强调了`start()`方法与`run()`方法的关系。同时,详细讲解了守护线程`daemon`属性,包括其设置时机、默认值以及如何影响程序退出条件。通过示例展示了守护线程在不同设置下的行为差异。
摘要由CSDN通过智能技术生成

1 run()方法

1.1 单个线程

在threading.Thread()类中有run()方法。

from time import ctime,sleep
import threading

# 定义自己类的功能
class MyThread(threading.Thread):
    def __init__(self,func,args,name = ""):
        threading.Thread.__init__(self)
        self.func = func
        self.args = args
        self.name = name
    # 调用start自动执行的函数
    def run(self):
        self.func(*self.args)
def player(song_file,time):
    for i in range(2):
        print("start player %s . %s"%(song_file,ctime()) )
    sleep(time)

threads = []

t = MyThread(player,('You and me .mp3',4),'Ipod')

threads.append(t)
t.start()
t.join()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值