UTF-8语言环境丢弃

I've wrote this test script:

<?php
ini_set('error_reporting', E_ALL );
putenv('LC_ALL=ru_RU.UTF-8');
putenv('LANG=ru_RU');
putenv('LANGUAGE=ru_RU');
setlocale (LC_ALL, "ru_RU.UTF-8");
for($i=0; $i<7; $i++){
    echo setlocale(LC_ALL, 0) . "<br>
";
    sleep(1);
}
?>

there is it output:

ru_RU.UTF-8
ru_RU.UTF-8
C
ru_RU.UTF-8
ru_RU.UTF-8
ru_RU.UTF-8
C

Why locale sometimes crashes? How to fix this?

From http://php.net/manual/en/function.setlocale.php...

Warning: The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().

I'm not a PHP expert but I guess an easy fix would be to start PHP with the right locale in the first place. You tagged this with gentoo. If you're using Gentoo but not Apache's mod_php then you should be able to use /etc/init.d/spawn-fcgi (which I wrote) from www-servers/spawn-fcgi to set LC_ALL. See /etc/conf.d/spawn-fcgi for details.