Python多线程和多进程

# -*- coding:utf-8 -*-

import threading,time,random
from multiprocessing import Pool,Process
import queue

def loopThread():
    print("subthreading name : %s"%(threading.current_thread().name))
    for i in range(10):
        print("%s >>> %s"%(threading.current_thread().name,i))
        time.sleep(2)
    print("%s end."%(threading.current_thread().name))

def threadPool():
    print("%s running..."%(threading.current_thread().name))
    t=threading.Thread(target=loopThread,name="threadTest")
    t.start()
    t.join()
    print("%s end."%(threading.current_thread().name))

def poolTest(args):
    print("POOL_JINCHENG %s"%(args))
    start=time.time()
    time.sleep(random.randint(1,10))
    end=time.time()
    print("POOL_JINCHENG %s cost %s times."%(args,end-start))

def testPool():
    p=Pool(4)
    for i in range(10):
        p.apply_async(poolTest,args=(i,))
    p.close()
    p.join()

if __name__=="__main__":
    threadPool()
    print("--------------------------分界线----------------------------")
    testPool()

  

posted @ 2020-11-19 22:47  Joooannes  阅读(69)  评论(0)    收藏  举报