php forking and using memcache
i am trying to learn about php, forking, sharing resources etc. while trying to understand the concept of these, i got a meaningless problem, i fixed (with luck) but didn't get the idea..
it may be a bug..
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$m->set('name', 'anil');
$workerCount = 3;
for($c=0; $c<$workerCount; $c++){
$pid = pcntl_fork();
if($pid === 0){
/* Children */
$m->get('name');
$name = $m->get('name');
echo 'This is child number : ' . getmypid() . PHP_EOL;
echo 'And this is name value in memcache : ' . $name . ' : ' . $m->getResultCode() . PHP_EOL . PHP_EOL;
开发者_Python百科 exit;
}else{
/* Parent */
pcntl_wait($status);
}
}
as you noticed, i call $m->get twice.. if i don't it doesn't work.
two calling result :
This is child number : 9684 And this is microtime in memcache : anil : 0
This is child number : 9685 And this is microtime in memcache : anil : 0
This is child number : 9686 And this is microtime in memcache : anil : 0
one calling result :
This is child number : 9721 And this is microtime in memcache : anil : 0
This is child number : 9722 And this is microtime in memcache : : 19
This is child number : 9723 And this is microtime in memcache : : 19
ps: $m->getResultCode() -> 19 is : Memcached::RES_SOME_ERRORS
you need to create your connections to memcached after the fork. otherwise you are sharing the same socket connection between multiple processes on the php side and memcache doesn't know that you are doing that.
精彩评论