天天看點

gevent的基本使用

import gevent
import time
from gevent import monkey
monkey.patch_all()

def kang():
    for i in range(5):
        print(i)
        time.sleep(5)

def dong(url):
    print(url)
    # time.sleep(2)

if __name__ == "__main__":
    for i in range(5):
        g1 = gevent.spawn(kang)
        print("kangdong")

    # g1.join()

    # for i in range(20):
    #     kang(i)
    #     dong(i)
           
  • gevent.spawn() #放進協程的“池子”
  • gevent.joinall() #啟動協程
  • monkey.patch_all() #隻要有延遲,先執行下一步