doushi1847 2012-05-07 23:13
浏览 89
已采纳

为什么while循环或其他长循环会阻止PHP中的后续请求?

Is there anything like a session ID that prevents the same session for having multiple requests/threads at a time? How does it works?

I have a loop in a method of one of my controller classes of my application that loops waiting for some actions (checking database).

I you need to see code, the following is an excerpt of the code inside the method:

// Times in hundredths of seconds.
$timestamp = number_format(microtime(TRUE), 2, '', '');
$closing_time = $timestamp + ($auditorium_lot_timeout * 100);

// Loop until time's up.
do
{
  usleep(250000); // Sleep 250ms to unload the CPU.
  if (Model_Event::count_new_bids())
  {
    // Etc, etc, etc...
    // Change closing time to wait 10 seconds more.
  }
} while (number_format(microtime(true), 2, '', '') < $closing_time);

// After time's up, continue with closing the bids definitely.
// Perform some more actions.

The method to which this excerpt belongs is requested by AJAX. It's purpose is to close bids on an auction but wait some seconds before closing to give chance for last bids. For that it loops a preconfigured amount of time (like 30 seconds) and while it's looping, the entire application can't recieve any other request from the same user. Why is that? It's blocking me very hard because this user is the operator/auctioneer of the live auction and must send "bid" requests for users that are not participating online (but presential in a real auditorium), while he triggered this loop.

Update: JS request code is abstracted by CanJS models.

  • 写回答

1条回答 默认 最新

  • douwen3083 2012-05-08 03:34
    关注

    That happens because built-in sessions mechanism locks session file if some request still uses it.

    So the possible solution for you is to release session in long living code by session_write_close()

    PS: oh, haven't seen there is such comment from @chris. As a justification it is not a stealing an answer I put this link, that proofs I answered similar question before: https://stackoverflow.com/a/6405685/251311

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?