开发者

CakePHP won't manage to handle two Ajax requests at once?

It's the third time that I'm editing this post, after more testing and debugging I keep finding out new stuff. Here my situation:

I have an method thinker that takes a while to process. Because of that I decided to call it via Ajax, and show an indicator to the user. Up to that point - works great.

Another thing I'm trying to do, is actually show the user the progress of "thinking", in percentage or just ticks, whatever.

It's not to difficult to do, because thinker has a loop, and in every iteration it saves new thoughts to the database.

So basically - the counter just show the number of "thoughts" (tied to the specific id).

The problem is, that the callback option doesn't get called.

Well - scratch that - it does get called, but only twice. Before the request (shows zero) and after the thinker is done thinking, when there is no use for it anymore (show the full amount).

Here's the simplified code:

<span id="indicator" style="display: none;"><img src="/img/ajax-loader.gif" alt="" /></span>
<span id="progress"></span>
<span id="clicker" style="cursor: pointer;">Do it!</span>

<script type="text/javascript">
//<![CDATA[
Event.observe('clicker', 'click', function(event) {
  new Ajax.Request(
    '/examples/thinker/123', 
    {asynchronous:true, evalScripts:true, onComplete:function(request, json) {Element.hide('indicator');}, onLoading:function(request) {Element.show('indicator');}}
    );
  new Ajax.PeriodicalUpdater(
    'progress', 
    '/examples/counter/123', 
    {asynchronous:true, evalScripts:true, frequency: 2}
    );
  });
//]]>
</script>

EDIT:

Through the use of Firebug XHR console I managed to get into this a little bit deeper. I actually went with jQuery (just to test, but it didn't help).

The problem is with CakePHP I guess. I mean - when I created an identical scenario with basic PHP it works at the same time. When I do it with Cake, the counter will get called, then the thinker will get called, and the counter will get called every two seconds, but will only respond after the thinker is done thinking.

Is it possible that Cake won't manage to handle the second request until the first one is finished?

Re开发者_高级运维gards, Paul


Well, as it turns out, the problem is in fact with Cake. By default, CakePHP stores sessions in file (default from php.ini actually). It sort of comes down to:

One user can't run two CakePHP instances at the same time with the default session storing option.

Changing:

Configure::write('Session.save', 'php');

to

Configure::write('Session.save', 'cache');

Seems to do the trick.


I don't think cake is to blame. It could eventually be your server, but that's unlikely. However, your script could have a lock on the database, that prevents it from being read again. That seems much more probable to me. I'm no expert at that, so I'm only guessing here, but I'd look into persistent connection, and force closing/opening a connection between each loop. There is also a thing to check with transactions, but again, I never ran into this kind of trouble.

Anyway, I'd try to rule out each component separately to investigate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜