0

I have this code:

import vlc
from time import sleep
from multiprocessing import Process

def lul():
    song = vlc.MediaPlayer("Himeringo - Shinitai-chan.mp3")
    song.audio_set_volume(50)
    song.play()

    sleep(5)
    while song.is_playing():
        print('&')
        sleep(5)

if __name__ == '__main__':
    k = Process(target=lul)
    k.daemon = False
    k.start()

And when I execute it, main process keeps running: Picture

I'm sure that highlighted guy is main, because when I added a while True loop that was printing things at the end and then killed this guy, it stopped printing this things (but proceeded with printing &s) and continued playing the song

My goal is to keep process with song alive, while there is nothing else running. And the main continuing as zombie isn't what I want.

I can kill it manually, and the song will keep playing, but that is not the way.

So, the question is: why main process stays alive when it reaches end line? Once I divided by zero, but it continued as zombie despite that fact: Picture

asdf asdf
  • 3
  • 3
  • With 428 questions on [multiprocessing terminate process](https://stackoverflow.com/search?q=multiprocessing+terminate+process) this is surely a duplicate. Please close – smci May 27 '18 at 07:21
  • You simply want to non-blocking fork a child process? or call lul() from __main__()? or what? Better to tell us what you actually want to do, not just complain that the code isn't doing it. – smci May 27 '18 at 07:49
  • @smci Chill man, I wasn't able to find a similar question (mostly because of my small english vocabulary, but I'm working on it), plus Mr. Zwinck - professional psychic - already gave me really good answer. How do I close the question? If it is done by choosing the best answer, then I've done it. – asdf asdf May 27 '18 at 11:18
  • asdfadf it's ok, it was closed already as duplicate. (That's a different thing to accepting an answer). Anyway I posted because there were already hundreds of questions on this and similar topics. Their titles and findability aren't great though, this is something that can be improved in future. – smci May 27 '18 at 11:34

1 Answers1

0

Python's multiprocessing always waits for the child to complete. But subprocess.Popen() does not. However, subprocess.Popen() launches any new program, not necessarily a Python one, and does not directly call a Python function. You could do something like Popen(['python', __file__, '--child']) and implement your script so that when sys.argv contains --child (hint: use argparse), it will just call lul().

However, there is no need to do any of this--your end goal here seems to be that lul() runs in a Python interpreter, and no extra Python interpreter remains. So don't start a second interpreter in the first place--simply call lul() and be done with it.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436