> php教程 > php手册 > 본문

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

WBOY
풀어 주다: 2016-06-06 19:47:22
원래의
1458명이 탐색했습니다.

当然,对多线程有深入理解的人都知道通过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);

                }

            }

        }

    }

}

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!