go轻松开启100万个协程
package main
import (
"fmt"
"sync"
"time"
)
var wx sync.WaitGroup
func main() {
for i:=0; i < 1000000; i++ {
wx.Add(1)
go func(i int){
defer wx.Done()
for {
fmt.Println(i)
time.Sleep(time.Second*1)
}
}(i)
}
wx.Wait()
}
python起送开启100万个协程
import asyncio
import time
async def say_after(index):
while 1:
await asyncio.sleep(1)
print(index)
tasks = []
async def main():
print(f"started at {time.strftime('%X')}")
for i in range(1000000):
tasks.append(asyncio.create_task(say_after(i)))
# await say_after(2, 'world')
await asyncio.gather(*tasks)
asyncio.run(main())