Skip to content

Instantly share code, notes, and snippets.

@chitoku-k
Created January 22, 2017 13:55
Show Gist options
  • Save chitoku-k/cbaa0b436f16ae55bbbbe4059857f835 to your computer and use it in GitHub Desktop.
Save chitoku-k/cbaa0b436f16ae55bbbbe4059857f835 to your computer and use it in GitHub Desktop.
ブロックしない fwrite(のはずが全然速くならなかった)
<?php
$str = str_repeat(PHP_VERSION, 200000);
$len = strlen($str);
$r = null;
$e = null;
$w = [];
$bytes = [];
foreach (range(1, 100) as $i) {
$w[] = $fp = fopen("test{$i}.txt", 'wb');
$bytes[(string)$fp] = $len;
stream_set_blocking($fp, false);
}
while (false !== $result = stream_select($r, $w, $e, 50)) {
foreach ($w as $i => $fp) {
$written = $bytes[(string)$fp] -= fwrite($fp, $str);
if ($written <= 0) {
unset($w[$i]);
}
}
if (!$w) {
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment