简体   繁体   English

如何在多处理中动态更改自身变量、参数、参数...?

[英]How to dynamically change self variables, parameters, args... in multiprocessing?

I don't know much of Python yet, but I'm trying to create an app that controls multiple streams of sound simultaneously (It has to do with binaural beats, noise and the brain).我还不太了解 Python,但我正在尝试创建一个同时控制多个声音流的应用程序(它与双耳节拍、噪音和大脑有关)。 Given that I would like to control the volume and state of each of the tracks separately, I think that I need to use multiprocessing module.鉴于我想分别控制每个轨道的音量和状态,我认为我需要使用多处理模块。 The first of those streams is a simple background music.这些流中的第一个是简单的背景音乐。 I would like the user to pause it whenever he wants and I'm using pygame module.我希望用户可以随时暂停它,并且我正在使用 pygame 模块。

import pygame
import time
import os
import multiprocessing

class play_music:
    def __init__(self, path="", name=""):
        self.state="" #pause, play, stop
        self.name=name #name of the song
        self.path=path #path of the song

    def play_music(self):
        path=self.path

        pygame.init()
        pygame.mixer.music.load(path)
        pygame.mixer.music.play()
        print (f"Playing {path}...")

        while True:
            
            if self.state=="pause":
                pygame.mixer.music.pause()
                self.state=""

                while self.state=="":
                    if self.state == "continue":
                        pygame.mixer.music.unpause()
                    elif self.state=="stop"():
                        pygame.mixer.music.stop()
                        break
            elif self.state=="stop":
                pygame.mixer.music.stop()
                break


def main():
    task = ""

    while not ( task == "meditacion" or task == "estudio"):
        task = input ("Introduce que vas a hacer (meditacion, estudio): ").rstrip()


    name ="non-existing track"

    while not (os.path.exists(f"musica/{task}/{name}")):
        name = input ("Introduce pista musical: ").rstrip()



    path = f"musica/{task}/{name}"
    print (f"Correct track. Path: {path}")
        

    music = play_music()
    music.path=path
    music.name=name


    p1 = multiprocessing.Process(target=music.play_music)
    p1.start()


    time.sleep(3) #for letting the process start correctly

    while True:
        music.state=input("pause, stop?: ")



if __name__=="__main__":
    main()

This doesn't work.这行不通。 The state doesn't get modified whenever I input pause or stop.每当我输入暂停或停止时,状态都不会被修改。 Any help is welcomed.欢迎任何帮助。 Thank you so much in advance.非常感谢你。

Since multiprocessing module creates separate Python processes, it is not possible to share variables in an easy way.由于multiprocessing模块创建了单独的 Python 进程,因此不可能以简单的方式共享变量。

In case you would like to share variables yet still parallelize some of your computing, you should look into the built-in threading module.如果您想共享变量但仍然并行化您的一些计算,您应该查看内置的threading模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM