简体   繁体   中英

how to use multithreading in php

I am now developing a website about sharing videos in the Internet . the flow is as follows: the user upload the video -> the server-side receives the video and uses ffmpeg to convert the video format to flv -> the user goes on doing other things in the site. now I want to establish another thread to do the convert work(use ffmpeg to convert the video format to flv) such that the user can do other thing without waiting the video converted to flv. but don't know how!

PHP does not support threads. You should use background tasks run by a cron script to do that.

You could have a table with the jobs to be processed, for example, containing the file name on disk, the status (pending, converting, ready), etc, then have a script take all the pending jobs, change their status to converting, convert the files, then change the status to ready. This way you can also present the information to the user, and you could have multiple scripts running if you wish to convert more than one file at the same time.

Make a queue of videos that need to be converted, and convert the videos one by one using a cron job for example. This queue can be stored in something like a message queue or database.

Using a database you can create a table containing the columns ID , StartDatetime , EndDatetime , Filename , Status , Owner and set those appropriately when a video is uploaded. When some user requests the status of any of his or her videos, you'll simply look up the status in the database, where (for example) 1 = added, 2 = currently being processed, 3 = done, 4 = error.

Now your cron job starts processing all items, one by one, where the status equals 1; when processing it updates it to 2, and afterwards to 3 or 4 appropriately.


By all fork/exec/run-on-background-like solutions where you immediately start processing the uploaded video, you're allowing all users to choke the server. What if multiple users simultaneously start processing a large video? You have no control over the separate processes, and all of them are running at the same time, probably heavily slowing down your server.

By processing items sequentially using one process (started through cron ), you can better manage the resources being used.

Looks like a job for a job server . Just add it as a background task

PHP is not multithreaded in any way, shape, or form. You can fake it with a few methods, but they're not true "threads":

  1. ignore_user_abort(TRUE) allows a script to keep running even after the user disconnects.
  2. exec() can fire up a new process at the shell level which can run in the background, but this will be a completely independent process and not controllable from the parent script without extra measures
  3. pcntl_fork() will create a clone of the current script which can then go on with the processing, but again will be nominally independent of the parent script. This requires the pcntl extension to be available and is unsafe to use in the context of a web request.

我认为人们通常会转向Gearman来完成这些任务: http//php.net/manual/en/book.gearman.php

You can spawn ffmpeg as a background process. See the detailed explanation here .

I suggest saving uploaded video file somewhere, then running ffmpeg on it, and display "Processing file.." until the file is created (but you don't get to wait for ffmpeg, just check if it has created the file).

Another option is to put the file in some directory, say /videos. Then create a cron job which will monitor the /videos folder and convert any new files.

Of course, you can think a more elaborate and fail-safe method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM