Comment le langage de programmation monothread PHP implémente-t-il les opérations multithread ?

PHP中文网
Libérer: 2023-03-15 15:16:01
original
2437 Les gens l'ont consulté

Tout le monde sait que le langage PHP lui-même ne peut être qu'un seul thread. Une fois qu'une étape de l'opération est terminée, l'étape suivante est exécutée.

Mais parfois, nous avons besoin d'opérations asynchrones et de plusieurs threads exécutés en même temps

Présentons comment PHP implémente les opérations multithreads

Le code est le suivant. suit : Pour référence uniquement

<?php
/**
 * @title:        PHP多线程类(Thread)
 * @version:    1.0
 * @author:        php.cn < web@php.cn >
 * @published:    2010-11-2
 * 
 * PHP多线程应用示例:
 *  require_once &#39;thread.class.php&#39;;
 *  $thread = new thread();
 *  $thread->addthread(&#39;action_log&#39;,&#39;a&#39;);
 *  $thread->addthread(&#39;action_log&#39;,&#39;b&#39;);
 *  $thread->addthread(&#39;action_log&#39;,&#39;c&#39;);
 *  $thread->runthread();
 *  
 *  function action_log($info) {
 *      $log = &#39;log/&#39; . microtime() . &#39;.log&#39;;
 *      $txt = $info . "\r\n\r\n" . &#39;Set in &#39; . Date(&#39;h:i:s&#39;, time()) . (double)microtime() . "\r\n";
 *      $fp = fopen($log, &#39;w&#39;);
 *      fwrite($fp, $txt);
 *      fclose($fp);
 *  }
 */
class thread {
    var $hooks = array();
    var $args = array();
    function thread() {
    }
    function addthread($func)
    {
        $args = array_slice(func_get_args(), 1);
        $this->hooks[] = $func;
        $this->args[] = $args;
        return true;
    }
    function runthread()
    {
        if(isset($_GET[&#39;flag&#39;]))
        {
            $flag = intval($_GET[&#39;flag&#39;]);
        }
        if($flag || $flag === 0)
        {
            call_user_func_array($this->hooks[$flag], $this->args[$flag]);
        }
        else
        {
            for($i = 0, $size = count($this->hooks); $i < $size; $i++)
            {
                $fp=fsockopen($_SERVER[&#39;HTTP_HOST&#39;],$_SERVER[&#39;SERVER_PORT&#39;]);
                if($fp)
                {
                    $out = "GET {$_SERVER[&#39;PHP_SELF&#39;]}?flag=$i HTTP/1.1\r\n";
                    $out .= "Host: {$_SERVER[&#39;HTTP_HOST&#39;]}\r\n";
                    $out .= "Connection: Close\r\n\r\n";
                    fputs($fp,$out);
                    fclose($fp);
                }
            }
        }
    }
}
$thread = new thread();
$thread->addthread(&#39;func1&#39;,&#39;info1&#39;);
$thread->addthread(&#39;func2&#39;,&#39;info2&#39;);
$thread->addthread(&#39;func3&#39;,&#39;info3&#39;);
$thread->runthread();
//说明:
//addthread是添加线程函数,第一个参数是函数名,之后的参数(可选)为传递给指定函数的参数。
//runthread是执行线程的函数。
Copier après la connexion

Cet article est fourni par le site Web chinois PHP et explique comment utiliser PHP pour simuler des fonctions multi-threading.

Adresse de l'article : http://www.php.cn/php-weizijiaocheng-377481.html

Apprenez la programmation sur le site PHP chinois www.php.cn

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!