Information

[스크랩] PHP 쓰레드 흉내내기

by LionHeart posted Dec 02, 2015
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄
http://w-shadow.com/blog/2008/05/24/improved-thread-simulation-class-for-php/ : 원본 참조링크 


바로 코드 테스트 들어감 
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
include_once("thread.class.php");
 
//쓰레드 구동 함수
function test($s, $s1, $sleep_time){               
    sleep($sleep_time);
    return sprintf("%s-->%s : sleep time : %s", $s, $s1, $sleep_time);              
}  
 
$program_start_time = microtime(true);
 
//쓰레드 생성
$thread1 = new Thread("localhost");
 
//쓰레드로 돌릴 함수를 등록 그리고 매개변수를 전달 한다. - 매개변수의 개수와 맞아야함
$thread1->setFunc('test', array("testString","testString1",2));
$thread1->start();
  
$thread2 = new Thread("localhost");
$thread2->setFunc('test', array("testString","testString1",2));
$thread2->start();
  
// 종료된 쓰레드의 응답을 기다림
while ( !$thread1->finished ||
            !$thread2->finished){
             
    $thread1->query();
    $thread2->query();      
}
  
//결과
printf("Thread1: %s <br>", $thread1->result);
printf("Thread2: %s <br>", $thread1->result);
printf("Total execution time : %s  seconds<br>", (microtime(true)-$program_start_time) );
/*
    동일 하게 2초를 사용 했고 전체 사용 시간이 2초 인것을 알수 있다. ㅎ_ㅎ
    Thread1 : testString-->testString1 : sleep time : 2
    Thread2 : testString-->testString1 : sleep time : 2
    Total execution time : 2.0561389923096 seconds
 */


글자가 깨지므로 코드 첨부 :

기본적인 원리는 이렇다 스레드 객체를 생성 할때에 호스트 이름만 적어주면 
현재 파일을 찾고 지정한 함수 이름을 호출을 하는데 매개변수 까지 전달 해서 호출 결과를 받아 올수 있다. 

쓰레드 클래스 에서는 HTTP 로 동시에 전체 호출 하고 결과는 차근 차근 받아온다. 

훔... 유용할듯 ..................................... ㅋ 
역시 외쿡 사람들이 잘하는 사람들이 많음 우찌 이런 사랑스러운 코드를.............................. 


URL: http://code.p-ark.co.kr/154