线程池

  • 线程池

from concurrent.futures import ThreadPoolExecutor,as_completed

import time

#为什么要线程池
#主线程可以获取某一个线程的状态或者某一个任务的状态,以及返回值
#当一个线程完成的时候,主线程立马知道
#funtures 可以让多线程和多进程编码接口一致

def get_html(times):
    time.sleep(times)
    print("get page {} sucess".format(times))
    return times

executor = ThreadPoolExecutor(max_workers=2)

#通过submit提交执行的函数到线程池中,sumbit是立即返回

task1 = executor.submit(get_html, (3)) #函数和参数

#done 方法用于判定某个任务是否完成

print(task1.done()) #False
time.sleep(4)

print(task1.done) #True

#result方法查看task函数执行的结构
print(task1.result()) #3



#线程池用as_completed获取任务结束的返回
from concurrent.futures import ThreadPoolExecutor,as_completed
import time

def get_html(times):
    time.sleep(times)
    print("get page {} success".format(times))
    return times + 1

executor = ThreadPoolExecutor(max_workers=2)

urls = [3,2,4]

all_task = [executor.submit(get_html,(url)) for url in urls]

for future in as_completed(all_task):
    data = future.result()
    print(data

)
  • 进程间通信 - Queue
import time


from multiprocessing import Process,Queue

def producer(queue):
    queue.put("a") #将a 写入到queue队列中
    time.sleep(2)

def consumer(queue):
    time.sleep(2)
    data = queue.get() # 获取queue队列中的信息,所有最后打印a
    print(data)

if __name__ == '__main__':
    queue = Queue(10)
    my_producer = Process(target=producer,args=(queue,))
    my_consumer = Process(target=consumer, args=(queue,))

    my_producer.start()
    my_consumer.start()
    my_producer.join()
    my_consumer.join()
    #最后打印a

  • 进程间通信 - Manager
import time

from multiprocessing import Process,Queue, Manager,Pool

def producer(queue):
    queue.put("a") #将a 写入到queue队列中
    time.sleep(2)

def consumer(queue):
    time.sleep(2)
    data = queue.get() # 获取queue队列中的信息,所有最后打印a
    print(data)

if __name__ == '__main__':
    #pool中的进程间通信需要使用manger中的queue
    queue = Manager().Queue(10)
    pool = Pool(2) #创建进程池

    pool.apply_async(producer, args=(queue,))
    pool.apply_async(consumer, args=(queue, ))

    pool.close()
    pool.join()
  • pipe实现进程间通信
#pipe实现进程间通信(只能两个进程之间)

from multiprocessing import Process,Pipe

def producer(pipe):
    pipe.send("derek")

def consumer(pipe):
    print(pipe.recv())
    
if __name__ == '__main__':
    receiv_pipe, send_pipe = Pipe()
    my_producer = Process(target=producer, args=(send_pipe, ))
    my_consumer = Process(target=consumer, args=(receiv_pipe, ))

    my_producer.start()
    my_consumer.start()
    my_producer.join()
    my_consumer.join()
posted @ 2021-02-07 14:19  Mr.zou  阅读(60)  评论(0编辑  收藏  举报