【发布时间】: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