python 多线程 and 多进程例子

2021-04-02 07:40:45 浏览数 (1)

代码语言:javascript复制
from queue import Queue
import threading
import requests
from multiprocessing import Process,Pool
q=Queue()
def pu():
    for x in range(999):
        q.put(x)

def ge():
    while True:
        if q.qsize()==0:
            break
        s = requests.get("http://666cc.cn")
        print(q.get(),s.status_code)


        time.sleep(5)

def run(num):
    for x in range(num):
        t1=threading.Thread(target=ge)
        print(t1.getName())
        t1.start()
    t1.join()

def thread(num):

    pu()
    run(num)
def multri(num):
    p = Pool(10)
    for x in range(num):
        p.apply_async(say, (x,))
    p.close()
    p.join()
def say(x):
    print(x)
    time.sleep(5)
if __name__ == '__main__':
    # multri(100) # 进程池
    thread(1000) # 线程池

0 人点赞