0

How am I able to run my line of code in the background whilst other part is running.I only want to run it from IDLE without the CLI. I want it something like this. Thanks in advance.

from playsound import playsound
playsound('The Music which i want run in the background')

#The code i want to run in the foreground

2 Answers 2

1

You can use Multiprocessing library to achieve this.

from playsound import playsound
from multiprocessing import Process


process_for_sound = Process(target=playsound, args=('The Music which i want run in the background',))
process_for_sound.start()
process_for_sound.join()

#The code you want to run in the concurrently.
Sign up to request clarification or add additional context in comments.

5 Comments

That is really helpful. How could I do it if I wanted to run something in the background other than playing music.
You can call any function of your choice by using this method. Please refer to the docs for much more advanced features.
I have tried your solution but only goes to concurrent code.
This is the solution that I've used for a similar problem of mine. If you could give a better explanation of what is required, I might be able to help you
I am making a game with turtle graphics and want the music to play in the background.
0
from threading import Thread

Thread(target=playsound, args=('\dir\to\music')).start()

'Concurrent code'
_________________

or you can simply:

playsound('\dir\to\music', 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.