arioman
arioman

Reputation: 87

Interrupt subprocess

So I have a loop executing video files on VLC. I want to make an executable file (.bat or .py I don't care) for running this code

for video in list:
   subprocess.run([vlc,'--play-and-exit','-f', video], shell=True)

I want the user to be able to interrupt this without ctrl+C (which only works if vlc is closed which also is a problem, because it is only closed for like 0.1 seconds)

I tried with a flag controlled by a button, but it does not work because vlc must be closed to control the flag (same problem as ctrl+c)

for video in list:
       subprocess.run([vlc,'--play-and-exit','-f', video], shell=True)
       if flag: 
          break

I don't want to close the program window to stop running the program

Upvotes: 0

Views: 568

Answers (1)

arioman
arioman

Reputation: 87

I used Popen instead of subprocess, so the video process starts in background, continuing with python code.

Then I looked for a function that returns 1 if Enter is pressed, if not waits 1 second and returns 0.

process = Popen([vlc,'--play-and-exit','-f', video])
                            while process.poll() is None:
                                    flag = readInput(text, False, 1)                                         
                                    if flag:
                                            break

the function readinput() was found here Keyboard input with timeout?

Upvotes: 1

Related Questions