-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblocking.php
105 lines (81 loc) · 2.49 KB
/
blocking.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
// Test blocking calls
// E.g. if you make a blocking IO call in the callback function, then you may not be able to
// Abort the spinner with ctrl-c.
require_once "vendor/autoload.php";
use Diversen\Spinner;
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
// Test of blocking calls.
// If you make a blocking IO call in the callback function, then you may not be able to
// Abort the spinner with ctrl-c.
//
// Start a php server in base directory
// php -S localhost:8000 -t examples/
//
// Curl example that will not block
function request($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch);
do {
$status = curl_multi_exec($mh, $active);
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);
while ($active && $status === CURLM_OK) {
if (curl_multi_select($mh) === -1) {
usleep(100);
}
do {
$status = curl_multi_exec($mh, $active);
} while ($status === CURLM_CALL_MULTI_PERFORM);
}
if ($status !== CURLM_OK) {
throw new Exception(curl_multi_strerror($status));
}
$result = curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
curl_close($ch);
return $result;
}
// Stream context example that will block
function stream_context () {
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => "User-Agent: MyAgent/1.0\r\n"
]
]);
// Set a timeout
stream_context_set_option($context, 'http', 'timeout', 5);
try {
$url = 'http://localhost:8000';
$res = file_get_contents($url, false, $context);
return $res;
} catch (Exception $e) {
return $e->getMessage();
}
}
$spinner = new Spinner(spinner: 'dots');
$res = $spinner->callback(function () {
// This will NOT block
// try {
// return request('http://localhost:8000');
// } catch (Exception $e) {
// return $e->getMessage();
// }
// This will block. You can not escape using ctrl-c
try {
return stream_context();
} catch (Exception $e) {
return $e->getMessage();
}
});
echo $res . "\n";