Implement PHP multithreading through WEB Servers

Source: Internet
Author: User
Of course, anyone who has a deep understanding of multithreading knows that multithreading implemented through WEB servers can only imitate some of the effects of multithreading, not in the true sense. But in any case, it can still meet some of our needs, and can still be used in the need of multi-threaded features. 12345678910111213141516

Of course, anyone who has a deep understanding of multithreading knows that multithreading implemented through WEB servers can only imitate some of the effects of multithreading, not in the true sense. But in any case, it can still meet some of our needs, and can still be used in the need of multi-threaded features. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Of course, anyone who has a deep understanding of multithreading knows that multithreading implemented through WEB servers can only imitate some of the effects of multithreading, not in the true sense.

But in any case, it can still meet some of our needs, and can still be used in the need of features similar to multithreading.

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 multithreading Application Usage:

* 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();

functionthread() {

}

functionaddthread($func)

{

$args= array_slice(func_get_args(), 1);

$this->hooks[] =$func;

$this->args[] =$args;

returntrue;

}

functionrunthread()

{

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, $size = count($this->hooks);$i < $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);

}

}

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.