0

I have a case looks like this: Here is my controller code:

//example controller:

public function runAction() {
    // a example code that run a long time
    $count = 0;
    while (true) {
        $count = $count + 1;
        Mage::register('testcount', $count);
        sleep(3);
    }

}

public function viewAction() {
    echo Mage::registry("testcount");
}

I want to view $count while "runAction" is running by using viewAction. But the viewAction loads forever. Any solution for me?

1
  • Mage::getSingleton("core/session")->setTestCount($count) and Mage::getSingleton("core/session")->getTestCount() did not work either.
    – Lan Nguyen
    Commented Jul 3, 2013 at 10:07

3 Answers 3

1

You could use the cache storage for this:

Set cache value:

Mage::app()->saveCache($count, 'my_counter', array(), 86400);

Read the cache:

Mage::app()->loadCache('my_counter')

Clear cache:

Mage::app()->removeCache('my_counter')
2

Mage::registry("testcount") will expire when your current request ends. Use the session to persist values.

In addition to Marius' catch of the while loop never ending - the reason your controller viewAction never returns is most likely due to filesystem session locks. If you have a long-running request it turns out that while your filesystem session file is locked, other processes cannot access it. Try switching your session type to DB or to Memcached/APC.

See below for more information about this phenomenon:

Long Running Admin Page Requests Blocking Other Requests

2

There is something wrong with your code.
First of all runAction will (try) run forever because of while(true), and second of all you will get an error because of this: Mage::register('testcount', $count);. The first time it works, the second time will give you an error because there is already a value in the registry for testcount.
On an other view, Mage::register() and Mage::registry() do not keep values between requests. This is just a way to set and get values 'globaly' in the same request. For passing values between requests use the session. Or write the value in a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.