• 线程


    import threading
    from time import ctime,sleep
    #
    #
    # def music(func):
    # for i in range(2):
    # print ("I was listening to %s. %s" %(func,ctime()))
    # sleep(4)
    #
    # def move(func):
    # for i in range(2):
    # print ("I was at the %s! %s" %(func,ctime()))
    # sleep(5)
    #
    # threads = [] #设置线程list
    # t1 = threading.Thread(target=music,args=(u'爱情买卖',)) #target 是线程函数的名称、 args是线程函数参数
    # threads.append(t1) # 添加线程到(线程)list 里面去
    # t2 = threading.Thread(target=move,args=(u'阿凡达',))
    # threads.append(t2)
    #
    # if __name__ == '__main__':
    # for t in threads: #开启线程
    # t.setDaemon(True) # 守护线程、如果设置了这句、就表明线程是不重要的、进程不需要等待子进程退出。就可以直接退出。 相反如果要等待线程执行完毕,在退出就不用执行这句。
    # t.start() #线程开始执行。
    # t.join() #这个作用就是、父线程、等待子线程结束执行。 作用:阻塞父线程完成。
    # #注意阻塞必须要在for 的外面。也就是说等待线程执行完毕、再去执行主进程 #一定是一个线程生成另一个线程。 所以t是一个父线程
    # print ("all over %s" %ctime())


    #------------------------------------------------------------------------
    ##用一个线程函数做判断
    import threading
    #from time import sleep, ctime
    #
    # def muisc(func):
    # for i in range(2):
    # print('Start playing: %s! %s' % (func, ctime()))
    # sleep(2)
    #
    #
    # def move(func):
    # for i in range(2):
    # print('Start playing: %s! %s' % (func, ctime()))
    # sleep(5)
    #
    #
    # def player(name): #选择线程的函数。
    # r = name.split('.')[1]
    # if r == 'mp3':
    # muisc(name)
    # else:
    # if r == 'mp4':
    # move(name)
    # else:
    # print
    # 'error: The format is not recognized!'
    #
    #
    # list = ['爱情买卖.mp3', '阿凡达.mp4']
    #
    # threads = []
    # files = range(len(list))
    #
    # # 创建线程
    # for i in files:
    # t = threading.Thread(target=player, args=(list[i],)) #添加线程函数名称、 和参数
    # threads.append(t)
    #
    # if __name__ == '__main__':
    # # 启动线程
    # for i in files:
    # threads[i].start()
    # for i in files:
    # threads[i].join()
    #
    # # 主线程
    # print('end:%s' % ctime())

    #------------------------------------------------------------------------------------------------------
    #用线程类

    import threading
    from time import sleep, ctime

    # coding=utf-8
    import threading
    from time import sleep, ctime


    class MyThread(threading.Thread):

    def __init__(self, func, args, name=''):
    threading.Thread.__init__(self)
    self.name = name
    self.func = func
    self.args = args

    def run(self):
    print(self.func, self.args)


    def super_play(file, time):
    for i in range(2):
    print('Start playing: %s! %s' % (file, ctime()))
    sleep(time)


    list = {'爱情买卖.mp3': 3, '阿凡达.mp4': 5}

    # 创建线程
    threads = []
    files = range(len(list))

    for k, v in list.items(): # 有几个参数返回几个线程。
    t = MyThread(super_play, (k, v), super_play.__name__) #利用线程类、前提要继承。 注意这个三个参数、 1、是开启线程的名字。 2、参数 3、是MyThread是内置方法
    threads.append(t)

    if __name__ == '__main__':
    # 启动线程
    for i in files:
    threads[i].start() #开始执行
    for i in files:
    threads[i].join() #阻塞

    # 主线程
    print('end:%s' % ctime())
  • 相关阅读:
    Python--面向对象编程
    Python--私有
    Python--格式化cookie为字典类型
    Python--异常处理
    Python--加密小练习
    bzoj 1774: [Usaco2009 Dec]Toll 过路费
    lougu T7983 大芳的逆行板载
    bzoj 1083(&vijos 1190): [SCOI2005]繁忙的都市 && bzoj 1601: [Usaco2008 Oct]灌水
    vijos 1083 小白逛公园
    51nod 1766 树上的最远点对
  • 原文地址:https://www.cnblogs.com/yuanjia8888/p/10102619.html
Copyright © 2020-2023  润新知