Heim  >  Artikel  >  php教程  >  通过WEB服务器来实现PHP多线程功能

通过WEB服务器来实现PHP多线程功能

WBOY
WBOYOriginal
2016-06-06 19:47:221459Durchsuche

当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程只能模仿多线程的一些效果,并不是真正意义上的多线程。 但不管怎么样,它还是能满足我们的一些需要的,在需要类多线程的功能方面还是可以采用这个类。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程只能模仿多线程的一些效果,并不是真正意义上的多线程。

但不管怎么样,它还是能满足我们的一些需要的,在需要类似多线程的功能方面还是可以采用这个类。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

/**

 *

 *  PHP多线程应用使用方法:

 *  require_once 'thread.class.php';

 *  $thread = new thread();

 *  $thread->addthread('action_log','a');

 *  $thread->addthread('action_log','b');

 *  $thread->addthread('action_log','c');

 *  $thread->runthread();

 *

 *  function action_log($info) {

 *      $log = 'log/' . microtime() . '.log';

 *      $txt = $info . "\r\n\r\n" . 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "\r\n";

 *      $fp = fopen($log, 'w');

 *      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['flag']))

        {

            $flag = intval($_GET['flag']);

        }

 

        if($flag || $flag === 0)

        {

            call_user_func_array($this->hooks[$flag], $this->args[$flag]);

        }

        else

        {

            for($i = 0, <code>$size = count($this->hooks); $i <code>$size; $i++)

            {

                $fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']);

                if($fp)

                {

                    $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1\r\n";

                    $out .= "Host: {$_SERVER['HTTP_HOST']}\r\n";

                    $out .= "Connection: Close\r\n\r\n";

                    fputs($fp,$out);

                    fclose($fp);

                }

            }

        }

    }

}

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn