go轻松开启100万个协程

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. )
  7. var wx sync.WaitGroup
  8. func main() {
  9. for i:=0; i < 1000000; i++ {
  10. wx.Add(1)
  11. go func(i int){
  12. defer wx.Done()
  13. for {
  14. fmt.Println(i)
  15. time.Sleep(time.Second*1)
  16. }
  17. }(i)
  18. }
  19. wx.Wait()
  20. }

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())