1

I recently installed my video script to a new server but I am seeing that it will start to convert 1 video (via mencoder) then before finishing it, it will try and convery another, and another, so it will be trying to convert 4+ videos at the same time causing the server to shut down. The script developer said:

"It converts each video in a PHP background process. There might be a way to limit the number of PHP background processes on your server and queue them."

So how is this done please?

Regards

4

2 回答 2

1

Use PHP Semaphores

You can use a shared counting Semaphore in PHP, and implement a queue with a cap on the no. of parallel executions. Semaphores are always the most recommended method for any form of concurrency control.

Using this you can easily configure and control the parallel executions of mencoder, and limit them as well.


Pseudocode

Begin
    init sem=MAX;

    wait(sem) //sem--, waits if sem=0, till atleast one process comes out of the critical section
    /*
        Critical Section
        where you execute mencoder
    */
    signal(sem) //sem++
End
于 2008-12-30T08:54:46.397 回答
0

Use some sort of lock. For example, use file locking on a directory so only one process at a time can use the resource.

This would require a wrapper script for the encoder which will wait for the lock to be released by the currently running encoder.

It should also be smart enough to detect when a lock wasn't freed if the encoder crashes, and release the lock.

Edit: My understanding of the problem was there were multiple invocations of the script, each calling mencoder. However, from the other response, it seems that it might be one invocation running all the processes in the background. Then I think the solution using Semaphores is better.

Edit: Looks like someone asked this question before:

best-way-to-obtain-a-lock-in-php

于 2008-12-30T08:43:16.077 回答