2

Here's the simplified example. I have this code:

from pygame import mixer
def say():
    mixer.init()
    mixer.music.load('/home/orif/Downloads/english.wav')
    mixer.music.play()
    while mixer.music.get_busy():
        time.sleep(0.2)

It displays some warnings but works perfectly. The warnings:

ALSA lib pcm_dsnoop.c:606:(snd_pcm_dsnoop_open) unable to open slave ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave

But if I run it in child process it won't:

def test():
    talk_process = Process(target=say)
    talk_process.start()
    print 'waiting for the process...'
    talk_process.join()
    print 'done'

It prints the same warnings, then:

waiting for the process...
Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/home/orif/Projects/example.py", line 178, in say
    mixer.init()
error: No available audio device
done

If I do the same but with threading, it runs fine. I have tried what's written here and here. In the former link the solution in the comments almost helped me. I initialized mixer just before running the subprocess:

mixer.init()
talk_process.start()

But now the sound doesn't play, and the waiting loop while mixer.music.get_busy() is endless

What is happening and how to solve it?

Community
  • 1
  • 1
Orif Khodjaev
  • 1,050
  • 1
  • 12
  • 32

1 Answers1

0

Alright, my bad, I didn't check everything before asking the question. The code snippet above is a bit incomplete. There were also several imports what I thought was irrelevant. I imported my module where I use SpeechRecognition. In the code there was a function declaration which looked like this:

def foo(m = sr.Microphone()):
    pass

At the moment of writing that code I thought, since Python executes everything in the last possible moment, it should work too. And when I ran that code, it did, so everything seemed to be fine. I was wrong.

At the moment of function declaration, that method call was not treated as "if no argument given, substitute with the result of this call", but it was actually called and the return value was used as an optional argument. I don't know what exactly went wrong with sound card while multiprocessing, but that's why I got those weird and obscure error messages.

Here's a little example to show what was causing the issue:

def foo():
    print 'TRIGGERED'
    return 42

def bar(a = foo()):
    print a

if __name__ == '__main__':
    pass

Though seems it should do nothing, this code actually prints TRIGGERED. So, instead of assigning values to optional arguments like that, I do something like this:

def foo(a = None):
    a = 'bar' if a is None else a
Orif Khodjaev
  • 1,050
  • 1
  • 12
  • 32