首先,我是初學者。我想要完成的是在腳本執行時播放音樂。它現在所做的是播放音樂,等到音樂結束,然后執行其余的代碼。那不是我想要的。這是我的代碼:
import os
import subprocess
import multiprocessing
import threading
from playsound import playsound
CurrentPath = os.path.dirname(os.path.normpath(__file__))
os.chdir(CurrentPath)
def music():
Music = "Music.mp4"
#subprocess.run(["ffplay", "-nodisp", "-autoexit", "-hide_banner", Music])
playsound("Music.mp4")
def other_things():
print("Hello World")
#musicp = multiprocessing.Process(target=music())
#restp = multiprocessing.Process(target=other_things())
musicp = threading.Thread(target=music())
restp = threading.Thread(target=other_things())
restp.start()
musicp.start()
就像你看到的那樣,我什至嘗試過多執行緒,但它仍然等到音樂結束后再進入其余代碼。
uj5u.com熱心網友回復:
不要呼叫 Thread 函式的目標引數中的函式 - 洗掉括號以參考函式,而不是它的回傳值
musicp = threading.Thread(target=music) # instead of music()
restp = threading.Thread(target=other_things) # instead of other_things()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/491159.html
上一篇:Java競態條件
下一篇:pySpark-->NoSuchTableException:在資料庫“默認”中找不到表或視圖“my_test_table_thread_1”
