js,python,go 协程对比
发送10个请求,将响应组合到一个列表中输出.
js
使用aiohttp发送请求.
async方式
import axios from 'axios'
const get_36_kr = async (page: number) => {
const url = `https://36kr.com/api/search-column/mainsite?per_page=1&page=${page}`;
return (await axios.get(url)).data;
}
(async () => {
// 生成任务列表
const tasks = Array.from(new Array(10).keys()).map(item => get_36_kr(item));
// 等待所有任务返回
const res = await Promise.all(tasks);
console.log(JSON.stringify(res, null, 2));
})()
promise方式
const get_36_kr = (page: number) => {
const url = `https://36kr.com/api/search-column/mainsite?per_page=1&page=${page}`;
return new Promise((resolve, reject) => axios.get(url).then(resp => resolve(resp.data)))
}
const tasks = Array.from(new Array(10).keys()).map(item => get_36_kr(item))
Promise.all(tasks).then(res => console.log(JSON.stringify(res, null, 2)))
python
使用aiohttp发送请求
import asyncio
import aiohttp
import json
async def get_36_kr(page, client):
url = f'https://36kr.com/api/search-column/mainsite?per_page=1&page={page}'
async with client.get(url, ssl=False) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as client:
# 并发执行任务
res = await asyncio.gather(*[get_36_kr(i, client) for i in range(10)])
print(json.dumps(res, indent=2))
if __name__ == '__main__':
asyncio.run(main())
golang
package main
import (
json "encoding/json"
"fmt"
ioutil "io/ioutil"
"net/http"
)
func httpGet(url string) (result []byte, err error) {
resp, err1 := http.Get(url)
if err1 != nil {
err = err1
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, err
}
func get36kr(page int, ch chan map[string]interface{}) {
var mapResult map[string]interface{}
url := fmt.Sprintf("https://36kr.com/api/search-column/mainsite?per_page=1&page=%d", page)
resp, err := httpGet(url)
if err == nil {
json.Unmarshal(resp, &mapResult)
ch <- mapResult
}
}
func main() {
// 使用通道进行通信
ch := make(chan map[string]interface{})
totalPage := 10
donePageCount := 0
var itemList []map[string]interface{}
for i := 1; i <= totalPage; i++ {
go get36kr(i, ch)
}
// 汇总结果
for {
donePageCount++
data := <-ch
itemList = append(itemList, data)
if donePageCount == totalPage {
str, _ := json.MarshalIndent(&itemList, "", " ")
fmt.Println(string(str))
break
}
}
}