2

I'm using GNU/Linux and python 2.7.3, I'm new on it. I'm trying to execute a long installed system process like ffmpeg using the Popen() command to avoid blocking the main python process. I can't make it work.

My first attempt was to use threads, but it is a bit complicated, because the graphical interface doesn't work properly (and I suppose there has to be a better way)

Then I tried fork, but it is like killing flies with tanks.

Now I'm trying to use Popen, I saw a lot of references to its use in the internet, but I'm not running it properly I think. Maybe I'm misunderstanding the examples.

I've tried:

##command is ffmpeg like that works ok
p = subprocess.Popen(command, shell=True)    

I've tried also:

p = subprocess.Popen(command,stdin=PIPE, shell=True)

Usually I get an error or a sequentcial behavior, the command ends and then the main program resumes. What am I missing?

The command part:

    global vidSource
    global srtSource
    global done
    size = "3"

    font = "/usr/share/fonts/truetype/freefont/FreeSerif.ttf"
    command = 'mencoder ' + '"' + vidSource + '"' + " -oac copy -ovc lavc -lavdopts threads=2 -sub " + "'" + srtSource + "'" + " -subcp -utf8 -font "+ '"'+ font+ '"' + " -subfont-text-scale " + size + " -o " + '"' + vidSource + '2'+'"' 
2
  • What error are you getting? You do know, Popen uses fork() (on linux) for creating new processes... If you didn't want to fork for some reason, Popen is not a viable solution. Commented Sep 26, 2012 at 14:10
  • Sorry, i forget to specify. I've tried to reproduce the error, but i get with the two comands i post a sequencial behaviour. :( Commented Sep 26, 2012 at 14:15

1 Answer 1

2

You should be on a good track. Just use

p = subprocess.Popen(command)

You can check whether the process has ended with a p.poll, as described in the documentation

Note that using shell=True is not recommended (for security reasons), so you should try not to use it unless you have a very good reason.

As described in the documentation, your command must be a list of arguments. If you build a long string, you should use shlex.split(command) to transform it into a string while preserving the " and forth (of course, you need a import shlex in your module).

Sign up to request clarification or add additional context in comments.

5 Comments

I get an error: Traceback (most recent call last): File "/home/fer/myapp/myapp/MyappWindow.py", line 129, in on_addSubtitle_clicked p = subprocess.Popen(command) File "/usr/lib/python2.7/subprocess.py", line 679, in init errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory This is the only way i get an error, so i think the command is oK. Thanks for the help!
No such file or directory.. means you are trying to access/execute something that doesn't exist
command = 'mencoder ' + '"' + vidSource + '"' + " -oac copy -ovc lavc -lavdopts threads=2 -sub " + "'" + srtSource + "'" + " -subcp -utf8 -font "+ '"'+ font+ '"' + " -subfont-text-scale " + size + " -o " + '"' + vidSource + '2'+'"' Where the vid sources are passed by user. I have tested it with a os.system call and are OK for it.
@ferbuntu: please post the commands in your question with the proper formatting, it's gettng hard to read them in comments.
@PierreGM -- The problem here is obvious. OP is passing a string to Popen which is being interpretted as executable to find. But, there is not executable named mencoder ".... Op needs to use p = subprocess.Popen(shlex.split(command)) (or I suppose use shell=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.