php Thread实例

admin 2021年9月14日15:08:37评论43 views字数 1226阅读4分5秒阅读模式

支持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

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年9月14日15:08:37
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   php Thread实例http://cn-sec.com/archives/542735.html

发表评论

匿名网友 填写信息