Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

php - Long polling locking up other AJAX calls

I'm looking to do long polling to "push" some data down to the client and I'm also making other unrelated AJAX calls to the server in parallel with the long polling. It appears that my other AJAX calls won't complete until the long poll has received a response (either from a response or timeout). When I step through the Javascript, it appears that the 2nd AJAX request is being sent at the proper time, but the response isn't being received until the long poll request gets a response. Any idea what is going on?

Here is the code for the long polling portion:

Server side:

    function getPlaylistTracksIfChanged($playlist_id, $numClientTracks) {
  $reportChange = false;
  for($i = 0; $i < 10; $i++) {
   $numServerTracks = $this->PlaylistTrack->find('count', array(
     'conditions' => array('playlist_id' => $playlist_id)
    )
   );

   if($numClientTracks != $numServerTracks) {
    $reportChange = true;
    break;

   }

   sleep(3);

  }

  if($reportChange) {
   $playlist_tracks = $this->PlaylistTrack->find('all', array(
    'conditions' => array('playlist_id' => $playlist_id),
    'order' => array('PlaylistTrack.position') 
    )
   );

   $this->set('playlist_tracks', $playlist_tracks);
   $this->layout = false;
   $this->render('show_playlist_tracks_list');

  } else {
   $this->autoRender = false;
   return 'false';
  }

 }

Client side:

function checkForChangesOnServer() {
 $.post('/getResultsIfChanged/' + playlist_id + '/' + $('#sortable_tracks').children().size(), function(results) {

  if(results == 'false') {
   //alert('no change');
  } else {
   //alert('change');
  }

  checkForPlaylistChangesOnServer();

 });
}

And a sample of another AJAX call:

Server side:

    function getLibraryTracksStartingWithLetter($user_id, $letter) {
  $results = $this->Track->find(
   'all',
   array(
    'conditions' => array(
     'user_id' => $user_id,
     'OR' => array(
      'Track.artist LIKE' => $letter . '%',
      'Track.name LIKE' => $letter . '%'
     )
    ),
    'order' => array('case when Track.artist = "" then 1 else 0 end', 'Track.artist', 'Track.name')
   )
  );

  $this->set('results', $results);
  $this->layout = false;
  $this->render('show_library_results_list');
 }

Client side:

    function loadLibraryResultsForLetter(letter) {
 highlightLetterFilter(letter);

 $.post('/getLibraryTracksStartingWithLetter/' + user_id + '/' + letter, function(results) {
  updateLibraryResults(results);
 });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems like you experienced the session file lock.

Perform session_write_close() (or corresponding function in cakephp) to close the session in the begin of the ajax endpoint.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...