服务端
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"time"
)
type Response struct {
Code int
ID string
Msg string
Data []string
}
func HandleHello(w http.ResponseWriter, req *http.Request) {
id := req.FormValue("id")
rsp := new(Response)
rsp.Code = 0
rsp.ID = id
rsp.Msg = "success"
rsp.Data = []string{"data1", "data2"}
ret_json, _ := json.Marshal(rsp)
time.Sleep(time.Second * 2)
io.WriteString(w, string(ret_json))
}
func main() {
http.HandleFunc("/hello", HandleHello)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
客户端
- 一个使用信号量Semaphore进行限制协程数量。
"""
有界信号量,限制并发数量方法
"""
import asyncio
import aiohttp
SEMAPHORE = asyncio.Semaphore(10)
async def get_http(url, i):
async with SEMAPHORE:
async with aiohttp.ClientSession() as session:
async with session.get(url) as res:
print(f"i: {i}, {res.status}, text: {await res.text()}")
def main():
loop = asyncio.get_event_loop()
url = 'http://127.0.0.1:8080/hello?id={}'
tasks = [get_http(url.format(i),i) for i in range(600)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
if __name__ == '__main__':
main()