Python---进阶---多线程---threading

一、

使用多线程去播放两个播放列表,一个是movie,一个是music

_thread

threading

 ------------------------------------------

import _thread as thread
import time
movie_list = ["斗破.mp4", "复仇者联盟.avi", "钢铁雨.rmvb", "xxx.mp4"]
music_list = ['周杰伦.mp3', '五月天.mp3']
movie_format = ['mp4', 'avi']
music_format = ['mp3']
def play(playlist):
    for i in playlist:
        if i.split(".") in movie_format:
            print("你现在收看的是:{}".format(i))
            time.sleep(3)
        elif i.split(".")[1] in music_format:
            print("你现在收听的是:{}".format(i))
            time.sleep(2)
        else:
            print("没有能播放的格式")
def thread_run():
    thread.start_new_thread(play, (movie_list, ))
    thread.start_new_thread(play, (music_list, ))
    while True:
        time.sleep()
   
if __name__ == "__main__":
    thread_run()

二、

import threading
import time
movie_list = ["斗破.mp4", "复仇者联盟.avi", "钢铁雨.rmvb", "xxx.mp4"]
music_list = ['周杰伦.mp3', '五月天.mp3']
movie_format = ['mp4', 'avi']
music_format = ['mp3']
def play(playlist):
    for i in playlist:
        if i.split(".")[1] in movie_format:
            print("你现在收看的是:{}".format(i))
            time.sleep(3)
        elif i.split(".")[1] in music_format:
            print("你现在收听的是:{}".format(i))
            time.sleep(2)
        else:
            print("没有能播放的格式")
           
def thread_run():
    threading.Thread(target=play, args=(movie_list, ))
    threading.Thread(target=play, args=(music_list, ))
   
   
   
if __name__ == "__main__":
    thread_run()
三、
 
原文地址:https://www.cnblogs.com/niaocaizhou/p/11064948.html