php Thread实例

  • A+
所属分类:安全博客

支持windows linux

安装配置参考
参考1
代码示例1
代码示例2

拿个 批量url转 ip 做例子:

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
64
65
66
67
<?php
class test_thread_run extends Thread
{
public $url;
public $data;

public function __construct($url)
{
$this->url = $url;
}

public function run()
{
if(($url = $this->url))
{
$this->data = gethostbyname($url);
}
}
}

function model_thread_result_get($urls_array)
{
foreach ($urls_array as $key => $value)
{
$thread_array[$key] = new test_thread_run($value);
$thread_array[$key]->start();
}

foreach ($thread_array as $thread_array_key => $thread_array_value)
{
while($thread_array[$thread_array_key]->isRunning())
{
usleep(10);
}
if($thread_array[$thread_array_key]->join())
{
$variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data;
}
}
return $variable_data;
}

$urls=file('hosts.txt');
//$print_r($urls);
$c= count($urls);
//$h[]=array(");
foreach ($urls as $u){
$urls_array[]=trim($u);
}

//print_r($urls_array);
$t = microtime(true);
$result = model_thread_result_get($urls_array);
//print_r($result);
echo "将$c 条url转换为 ip\n";
$e = microtime(true);
echo "多线程耗时:".($e-$t)."\n";

$t = microtime(true);
foreach ($urls_array as $value)
{
gethostbyname($value);
}

$e = microtime(true);
echo "单线程耗时:".($e-$t)."\n";
?>

Source:wolvez.club | Author:wolvez

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: