Home > Blockchain >  Python, how to execute a line of code without it stopping the rest of the code from executing?
Python, how to execute a line of code without it stopping the rest of the code from executing?

Time:06-12

first of all, im a beginner. Want i want to accomplish is that music plays while the script is executing. What it does right now it plays the music, waits until the music is over and then executes the rest of the code. That is not what i want. Here my Code:

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

LIke you can see i even tried multithreading but it still waits until the music is over before it goes to the rest of the code.

CodePudding user response:

Don't call the functions in the target parameter of the Thread function - delete the brackets to reference the function, not its return value

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