python多进程线程用法

python多进程线程用法

前言

join作用是阻塞主进程/主线程执行

进程

import multiprocessing
import time
import random

def run(flag):
    n = random.randint(1, 10)
    print(flag, "begin:", n)
    time.sleep(n)
    print(flag, "end")


def main():
    print("main begin")
    p_list = []
    for i in range(5):
        p = multiprocessing.Process(target=run, kwargs={"flag": i})
        p_list.append(p)
        # # 作用:默认是False,代表父进程退出后,子进程继续执行,且须放在start前执行。True,代表父进程退出后,子进程也退出。
        # p.daemon = True
        p.start()


    for p in p_list:
        # 作用:主进程等待子进程处理完毕后退出,否则主进程不等子进程处理完成就退出,子进程放在后台跑
        p.join()

    print("main end")

if __name__ == '__main__':
    main()

进程池

方案一

import multiprocessing
import time
import random

def run(flag):
    n = random.randint(1, 10)
    print(flag, "begin:", n)
    time.sleep(n)
    print(flag, "end")


def main():
    print("main begin")
    pool = multiprocessing.Pool(5)
    for i in range(5):
        pool.apply_async(func=run, kwds={"flag": i})
    pool.close()
    # 作用:调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数是让主进程等待所有子进程结束
    pool.join()
    print("main end")

if __name__ == '__main__':
    main()

方案二

from concurrent.futures import ProcessPoolExecutor
import concurrent
import time
import random

def run(flag):
    n = random.randint(1, 10)
    print(flag, "begin:", n)
    time.sleep(n)
    print(flag, "end")

def main():
    print("main begin")
    with ProcessPoolExecutor(5) as executor:
        futures = []
        for i in range(5):
            futures.append(executor.submit(run, **{"flag": i}))
        for future in concurrent.futures.as_completed(futures):
            future.result()
    print("main end")

if __name__ == '__main__':
    main()

线程

import threading
import time
import random

def run(flag):
    n = random.randint(1, 10)
    print(flag, "begin:", n)
    time.sleep(n)
    print(flag, "end")

def main():
    print("main begin")
    t_list = []
    for i in range(5):
        t = threading.Thread(target=run, kwargs={"flag": i})
        t.start()

    for t in t_list:
        # 作用:阻塞主进程,让其等待子线程处理完毕
        t.join()

    print("main end")

if __name__ == '__main__':
    main()

线程池

from concurrent.futures import ThreadPoolExecutor
import concurrent
import time
import random

def run(flag):
    n = random.randint(1, 10)
    print(flag, "begin:", n)
    time.sleep(n)
    print(flag, "end")

def main():
    print("main begin")
    with ThreadPoolExecutor(5) as executor:
        futures = []
        for i in range(5):
            futures.append(executor.submit(run, **{"flag": i}))
        for future in concurrent.futures.as_completed(futures):
            future.result()
    print("main end")

if __name__ == '__main__':
    main()

协程

import asyncio
import random


async def run(flag):
    n = random.randint(1, 10)
    print(flag, "begin:", n)
    await asyncio.sleep(n)
    # time.sleep(n)
    print(flag, "end")

async def main():
    task_lists = []
    for i in range(5):
        task = asyncio.create_task(run(i))
        task_lists.append(task)

    for task in task_lists:
        await task


if __name__ == '__main__':
    asyncio.run(main())

本文作者:朝圣

本文链接:www.zh-noone.cn/2021/5/python多进程线程用法

版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0许可协议。转载请注明出处!

curl请求问题
0 条评论