douxugu5836 2010-04-20 23:13
浏览 109
已采纳

PHP脚本什么时候结束?

In my mind, a web app something that runs continuously; therefore, I'm confused by documentation pages that talk about the "end" of a PHP script (eg this one). Such references seem to refer to the end of each web request, but if the script ends there, doesn't that mean that the OS has to setup a whole new process for each request? That seems unlikely, because spinning up a whole new process is expensive, and be very inefficient for the whole site.

  • 写回答

2条回答 默认 最新

  • drgc9632 2010-04-20 23:16
    关注

    There's a distinction to make here: PHP is not a web server. Therefore, the end of a PHP script is irrelevant to the actual end of the web server (or at least, most of the time).

    Web server software that continuously run, like IIS, Apache or Lighttpd can execute PHP scripts without ending themselves. These scripts, at the end, do cease to run. How web servers set up the execution environment for the script depends on the approaches taken. For instance, Apache 2 can indeed spawn a child process for each web request, though it does not set up again the whole web server each time: there's a master process that commands the children about execution settings and parameters.

    Therefore, a PHP script ends when the web request ends; that is, when the script reaches a statement that makes it stop (like a fatal error, an exit statement or the like) or when it reaches the actual end of the script source.


    EDIT About the efficiency of spawning a new process for new requests.

    Nowadays, servers don't spawn a separate process for PHP itself: they use the PHP library to embed the language and work with it. However, servers are indeed free to spawn a process for every HTTP request they receive. Apache does it with its mpm_prefork module.

    While spawning a new process is indeed expensive, it's potentially nothing when compared to the cost of the script itself, which can last tenths of seconds, which is much, much more than just spawning a process.

    Also, servers usually have to serve many clients at once; therefore, it is unthinkable to just treat every request sequentially. They must be run concurrently, and that leaves implementors two options: threads or processes. PHP not being thread-safe on many platforms, it is often preferable to run it in a separate process.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值