gpt4 book ai didi

ruby - 使用 Thin 或根本不进行流式传输的缓慢流式传输

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:31 25 4
gpt4 key购买 nike

我写了一个小脚本来使用 Sinatra 和 Thin 按字节流式传输文件:

#!/usr/bin/env ruby

require 'sinatra'
require "sinatra/streaming"

get '/' do
stream do |out|
File.open("/usr/share/doc/ia32-libs/copyright", "r") do |fd|
fd.each_byte do |byte|
break if out.closed?
putc byte.chr
out << byte.chr unless out.closed?
end
end
end
end

这按预期工作:

% ruby streamer.rb
== Sinatra/1.3.2 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop


% curl http://127.0.0.1:4567

文件内容在两边都写到stdout

如果我删除 putc byte.chr 行,服务器就会崩溃并被 kill -9 杀死


如果我将此脚本更改为模块化样式并添加 config.ru,则会出现另一个意外行为:

#!/usr/bin/env ruby

require 'sinatra/base'
require "sinatra/streaming"

class TestStreamer < Sinatra::Base
helpers Sinatra::Streaming

get '/' do
stream do |out|
File.open("/usr/share/doc/ia32-libs/copyright", "r") do |fd|
fd.each_byte do |byte|
break if out.closed?
# putc byte.chr
out << byte.chr unless out.closed?
end
end
end
end
end

配置.ru:

$:.unshift(File.join(File.dirname(__FILE__)))

require 'rubygems'
require 'sinatra/base'
require 'streamer_modular'

map '/' do
run TestStreamer
end

它启动了:

% thin start
>> Using rack adapter
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop

但是当我发出请求时,文件在两边发送的速度都非常慢,并在 30 秒左右后关闭。在这种情况下,Sinatra 不会崩溃。

% time curl -v http://127.0.0.1:3000
* About to connect() to 127.0.0.1 port 3000 (#0)
* Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
> Host: 127.0.0.1:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Frame-Options: sameorigin
< X-XSS-Protection: 1; mode=block
< Content-Type: text/html;charset=utf-8
< Connection: close
< Server: thin 1.4.1 codename Chromeo
<
* Closing connection #0
This package was cre
curl -v http://127.0.0.1:3000 0,01s user 0,00s system 0% cpu 32,559 total

当我删除 stream-helper 时,文件会立即写入服务器端的标准输出:

#!/usr/bin/env ruby

require 'sinatra/base'
require "sinatra/streaming"

class TestStreamer < Sinatra::Base
helpers Sinatra::Streaming

get '/' do
# stream do |out|
begin
File.open("/usr/share/doc/ia32-libs/copyright", "r") do |fd|
fd.each_byte do |byte|
# break if out.closed?
putc byte.chr
# out << byte.chr unless out.closed?
end
end
end
end
end

% ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

% gem list

*** LOCAL GEMS ***

backports (2.6.2)
bundler (1.1.5, 1.1.3)
daemons (1.1.8)
eventmachine (0.12.10)
rack (1.4.1)
rack-protection (1.2.0)
rack-test (0.6.1)
sinatra (1.3.2)
sinatra-contrib (1.3.1)
thin (1.4.1)
tilt (1.3.3)

% gem update
Updating installed gems
Nothing to update

% ls -al /usr/share/doc/ia32-libs/copyright
-rw-r--r-- 1 root root 607403 2. Jan 2012 /usr/share/doc/ia32-libs/copyright

% head -n2 /usr/share/doc/ia32-libs/copyright
This package was created by Daniel Jacobowitz <dan@debian.org> on Sun, Aug
8th, 2004. It was based on the ia32-libs package by Bdale Garbee

我错过了什么吗?你能重现这个吗?也许能为我找到解决方法?

最佳答案

在这种模式下,在 Thin 上逐字节流式传输效率有点低,因为 Thin 是基于 EventMachine 的,因此需要大量的调度和延迟(因此 Mike 评论流式传输不适用于 Thin,Mike 可能有 Rails 背景Thin 上的此类流式传输确实不起作用)。

但是,Thin(和任何 Rack 服务器)应该能够直接流式传输文件:

get '/' do
File.open("/usr/share/doc/ia32-libs/copyright", "r")
end

关于ruby - 使用 Thin 或根本不进行流式传输的缓慢流式传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11690752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com