Getting an AssertionError when I use the multiprocessing module

801 Views Asked by At

The relevant code is actually from a separate thread.

Here it is anyway

import multiprocessing
from playsound import playsound

p = multiprocessing.Process(target=playsound, args=("file.mp3",))
p.start()
input("press ENTER to stop playback")
p.terminate()

When I run this exact code, I get the following error:

Traceback (most recent call last):
  File "/Users/omit/Developer/omit/main2.py", line 22, in <module>
    p2 = multiprocessing.Process(printStatus)
  File "/opt/homebrew/Cellar/[email protected]/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 82, in __init__
    assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now
(yt-proj) omit@omit-Air youtube proj % /opt/homebrew/bin/python3 "/Users/omit/Developer/omit/main2.py"
Traceback (most recent call last):
  File "/Users/omit/Developer/omit/main2.py", line 22, in <module>
    p2 = multiprocessing.Process(printStatus)
  File "/opt/homebrew/Cellar/[email protected]/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 82, in __init__
    assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now

What could be the problem?

2

There are 2 best solutions below

0
On

The code to start the multiprocessing must never be "top level". It must only be run if this is the main thread.

def main():
    p = multiprocessing.Process(target=playsound, args=("file.mp3",))
    p.start()
    input("press ENTER to stop playback")
    p.terminate()

if __name__ == "__main__":
    main()
0
On

Your problem, as seen in your error message is in the p2 variable. You are creating it like this:

p2 = multiprocessing.Process(printStatus)

That is a problem because the first positional argument is the group parameter. You need to change it to this:

p2 = multiprocessing.Process(target=printStatus)

Here are the python docs for multiprocessing.Process.

The relevant paragraph (emphasis mine):

class multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)
Process objects represent activity that is run in a separate process. The Process class has equivalents of all the methods of threading.Thread.
The constructor should always be called with keyword arguments. group should always be None; it exists solely for compatibility with threading.Thread. target is the callable object to be invoked by the run() method.

As shown above, your problem is coming from trying to create a Process object without using keyword arguments, and it is assigning something to the group parameter.