英文:
why is fasthttp like single process?
问题
requestHandler := func(ctx *fasthttp.RequestCtx) {
time.Sleep(time.Second * time.Duration(10))
fmt.Fprintf(ctx, "Hello, world! Requested path is %q", ctx.Path())
}
s := &fasthttp.Server{
Handler: requestHandler
}
if err := s.ListenAndServe("127.0.0.1:82"); err != nil {
log.Fatalf("error in ListenAndServe: %s", err)
}
多个请求,每个请求花费时间为 X*10 秒。
fasthttp 是单进程的吗?
两天后...
对不起,我之前的问题描述得不够清楚。我的问题是由浏览器引起的,浏览器通过同步方式请求相同的 URL,这让我误以为 fasthttp web 服务器是通过同步方式处理请求的。
英文:
requestHandler := func(ctx *fasthttp.RequestCtx) {
time.Sleep(time.Second*time.Duration(10))
fmt.Fprintf(ctx, "Hello, world! Requested path is %q", ctx.Path())
}
s := &fasthttp.Server{
Handler: requestHandler
}
if err := s.ListenAndServe("127.0.0.1:82"); err != nil {
log.Fatalf("error in ListenAndServe: %s", err)
}
multiple request,and it cost time like X*10s.
fasthttp is single process?
after two days...
I am sorry for this question,i describe my question not well.My question is caused by the browser,the browser request the same url by synchronization, and it mislead me, it make think the fasthttp web server hanlde the request by synchronization.
答案1
得分: 3
我认为你的问题是关于fasthttp是否能够同时处理客户端请求。我相信任何服务器(包括fasthttp)都可以并发处理客户端请求。你应该编写一个测试/基准测试,而不是通过多个浏览器手动访问服务器。以下是一个示例测试代码:
package main_test
import (
"io/ioutil"
"net/http"
"sync"
"testing"
"time"
)
func doRequest(uri string) error {
resp, err := http.Get(uri)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return nil
}
func TestGet(t *testing.T) {
N := 1000
wg := sync.WaitGroup{}
wg.Add(N)
start := time.Now()
for i := 0; i < N; i++ {
go func() {
if err := doRequest("http://127.0.0.1:82"); err != nil {
t.Error(err)
}
wg.Done()
}()
}
wg.Wait()
t.Logf("Total duration for %d concurrent request(s) is %v", N, time.Since(start))
}
在我的电脑上运行的结果是:
fasthttp_test.go:42: Total duration for 1000 concurrent request(s) is 10.6066411s
你可以看到答案是不,它可以并发处理请求。
更新:
如果请求的URL相同,你的浏览器可能会按顺序执行请求。参考https://stackoverflow.com/questions/9189591/multiple-ajax-requests-for-same-url。这解释了为什么响应时间是X*10s
。
英文:
I think instead of fasthttp is single process?, you're asking whether fasthttp handles client requests concurrently or not?
I'm pretty sure that any server (including fasthttp) package will handle client requests concurrently. You should write a test/benchmark instead of manually access the server through several browsers. The following is an example of such test code:
package main_test
import (
"io/ioutil"
"net/http"
"sync"
"testing"
"time"
)
func doRequest(uri string) error {
resp, err := http.Get(uri)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return nil
}
func TestGet(t *testing.T) {
N := 1000
wg := sync.WaitGroup{}
wg.Add(N)
start := time.Now()
for i := 0; i < N; i++ {
go func() {
if err := doRequest("http://127.0.0.1:82"); err != nil {
t.Error(err)
}
wg.Done()
}()
}
wg.Wait()
t.Logf("Total duration for %d concurrent request(s) is %v", N, time.Since(start))
}
And the result (in my computer) is
> fasthttp_test.go:42: Total duration for 1000 concurrent request(s) is 10.6066411s
You can see that the answer to your question is No, it handles the request concurrently.
UPDATE:
In case the requested URL is the same, your browser may perform the request sequentially. See https://stackoverflow.com/questions/9189591/multiple-ajax-requests-for-same-url. This explains why the response times are X*10s
.
答案2
得分: 0
非常抱歉,我理解你的问题描述得不够清楚。你的问题是由浏览器引起的,浏览器通过同步方式请求相同的URL,这让你误以为fasthttp web服务器是通过同步方式处理请求的。
英文:
I am sorry for this question,i describe my question not well.My question is caused by the browser,the browser request the same url by synchronization, and it mislead me, it make think the fasthttp web server hanlde the request by synchronization.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论