多处理 - 为每个工作人员传递共享队列和唯一编号

Multiprocessing - pass shared Queue and unique number for each worker

我无法完全找到代码的解决方案,在该代码中我传递给每个工作人员共享队列,但也为每个工作人员分配一个编号。

我的代码:

想法是创建多个频道来放置音频歌曲。每个通道都必须是唯一的。所以如果一首歌到了,我会把它放到可用的频道

from multiprocessing import Pool,Queue
from functools import partial
import pygame
queue = Queue()


def play_song(shared_queue, chnl):

    channel = pygame.mixer.Channel(chnl)
    while True:
        sound_name = shared_queue.get()
        channel.play(pygame.mixer.Sound(sound_name))
    


if __name__ == "__main__":
    channels= [0,1, 2, 3, 4]
    
    func = partial(play_song,queue)
    p = Pool(5,func, (channels,))

这段代码当然不会return任何错误,因为它是多处理的,但问题是channels被整体传递给了play_song列出而不是映射到所有工作人员。

所以基本上不是每个工人都像这样初始化通道:

channel = pygame.mixer.Channel(0) # each worker would have number from list so 1,2,3,4

我收到了

channel = pygame.mixer.Channel([0,1,2,3,4]) # for each worker

我试过玩部分功能,但没有成功。

我成功地使用了 pool.map 功能,但是虽然我可以从频道列表中传递个人号码,但我无法在工作人员之间共享队列

最终我找到了不需要线程或多处理的 Pygame 问题的解决方案。


问题背景:

我正在使用 Pyaudio,因为它对音频的级别很低 api,所以我在同时混合多种声音时遇到了问题,而且一般来说。原因是:

1)同时启动多个流或同时提供这些流(看起来像硬件问题)并不容易(也许不可能)

2) 基于 1) 我尝试了不同的态度 - 有一个流来自不同声音的音频波在进入流之前被加起来 - 这有效但它不可靠,因为加起来音频波并不真正兼容 - 添加太多波导致 'sound cracking' 因为振幅太高。

基于1)2) 我想在不同的进程中尝试 运行 流,因此这个问题。


Pygame解法(单处理):

for sound_file in sound_files:
    availible_channel = pygame.mixer.find_channel() #if there are 8 channels, it can play 8 sounds at the same time
    availible_channel.play(sound_file )

如果 sound_files 已经加载,这将给出几乎同时的结果。

多处理解决方案

感谢 Darkonaut 指出了多处理方法,我设法回答了我最初关于多处理的问题,我认为这个问题已经在 Whosebug 上得到了回答,但我会把它包括在内。

示例未完成,因为我最后没有使用它,但它满足了我对具有 共享队列 但具有 不同参数的进程的初始要求

import multiprocessing as mp

shared_queue = mp.Queue()


def channel(que,channel_num):
    que.put(channel_num)

if __name__ == '__main__':
    processes = [mp.Process(target=channel, args=(shared_queue, channel_num)) for channel_num in range(8)]

    for p in processes:
        p.start()

    for i in range(8):
        print(shared_queue.get())
    for p in processes:
        p.join()