簡體   English   中英

Python,如何在不停止其余代碼執行的情況下執行一行代碼?

[英]Python, how to execute a line of code without it stopping the rest of the code from executing?

首先,我是初學者。 我想要完成的是在腳本執行時播放音樂。 它現在所做的是播放音樂,等到音樂結束,然后執行其余的代碼。 那不是我想要的。 這是我的代碼:

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()

就像你看到的那樣,我什至嘗試過多線程,但它仍然要等到音樂結束后再進入其余的代碼。

不要調用 Thread 函數的目標參數中的函數 - 刪除括號以引用函數,而不是它的返回值

musicp = threading.Thread(target=music) # instead of music()
restp = threading.Thread(target=other_things) # instead of other_things()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM