正在运行脚本的一部分而其他部分正在运行-python

时间:2018-11-10 18:48:14

标签: python

我如何在其他部分运行的同时在后台运行我的代码行。我只想从没有CLI的IDLE中运行它。我想要这样的东西。预先感谢。

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

#The code i want to run in the foreground

2 个答案:

答案 0 :(得分:1)

您可以使用Multiprocessing库来实现此目的。

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.

答案 1 :(得分:0)

from threading import Thread

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

'Concurrent code'
_________________

或者您可以简单地:

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