【问题标题】:Force connection close lighttpd强制连接关闭 lighttpd
【发布时间】:2020-04-02 14:17:43
【问题描述】:

输出发送到客户端后,我需要在后台运行代码一段时间。
但是连接会阻塞并且在作业完成之前不会关闭。

看过了:
How do I close a connection early?
https://www.php.net/manual/en/features.connection-handling.php#71172
(和其他链接的答案)

我最终得到了代码:

    // credit : https://stackoverflow.com/questions/138374/how-do-i-close-a-connection-early#141026
    //          and related sources
    // broken on : lighttpd/1.4.53 - php/7.1.3

    $size=ob_get_length();
    header("Content-Length: $size",1);
    header("Connection: close",1);
    header("Content-Encoding: none\r\n");

    // flush all output
    ob_end_flush();
    ob_flush();
    flush();
    ob_end_clean();

    // if you're using sessions, this prevents subsequent requests
    // from hanging while the background process executes
    if (session_id()) session_write_close();

    ... some code for 10 seconds here

我都试过了:
server.max-keep-alive-idle=0
server.max-keep-alive-requests = 0
取自:
lighttpd force close connection
https://serverfault.com/questions/283377/force-connection-close-lighttpd
(放入 /etc/lighttpd/lighttpd.conf)

我的设置:
lighttpd/1.4.53
php/7.1.3
Firefox 68.6.0esr(32 位)
树莓派 4
树莓派
uname -a : Linux raspberrypi 4.19.97-v7l+ #1294 SMP Thu Jan 30 13:21:14 GMT 2020 armv7l GNU/Linux

启用 lighttpd 模块:
10-accesslog.conf
10-fastcgi.conf
10-rewrite.conf
15-fastcgi-php.conf
90-javascript-alias.conf

目的:
这是简单的 MUD 游戏代码。
每秒刷新一次
它是由玩家在游戏中的存在定期启动的:

1] 脚本会处理玩家的命令,
然后应该关闭连接以免打扰播放器的浏览器
2] 检查锁定文件,如果它是免费的 - 锁定它
3] 然后留在后台进行 10 次更新
之后,文件被解锁,
所以另一个玩家的命令可以开始另一个 bg 工作

【问题讨论】:

  • 您能否通过显示更多代码来更详细地说明您要完成的工作?什么在执行这段代码?
  • @Nibb 当然.. 我在这里隔离了功能示例:pastebin.com/WBTDKVVC
  • @Nibb .. 抱歉,我现在意识到,我的问题确实没有很好地说明......在问题中添加了“目的”

标签: php http-headers lighttpd httpconnection


【解决方案1】:

如果您的代码需要在发送 CGI 响应后进行处理,那么您的代码需要在单独的执行上下文中进行处理,因为 CGI 已经完成当它发送 CGI 响应时它的工作,然后 lighttpd 将向您的脚本发送一个 SIGTERM 信号。

在发送 CGI 响应后运行代码的一种方法是在发送 CGI 响应后在后台运行代码。见https://www.php.net/manual/en/function.posix-setsid.php

另一种方法是让您的 CGI 将作业放入作业队列并拥有一个独立于 CGI 的独立守护程序来处理队列,例如使用 redis 服务器。

【讨论】:

  • 它是为了在没有这种级别的控制(付费托管)的服务器上运行,所以我想出了这个自启动的背景东西..我忘了提到它,对不起
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-26
  • 2012-11-03
  • 2012-11-19
相关资源
最近更新 更多