python线程池和进程池的多参数调用

from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time
def calctime(x):
    start = time.perf_counter()
    x()
    end = time.perf_counter()
    return("time cost is", end-start)
def fun(x):
    a, b = x
    time.delay(2)
    return('a is ', a, 'b is ', b)
def muti_thread1():
    with ThreadPoolExecutor() as pool:
        data = [(x, x) for x in range(50)]
        results = pool.map(fun, data)
def thread():
    data = [(x, x) for x in range(50)]
    for i in data:
        fun(i)
def muti_process():
    with ProcessPoolExecutor() as pool:
        data = [(x, x) for x in range(50)]
        results = pool.map(fun, data)
if __name__ == '__main__':
    print(calctime(muti_thread1))
    print(calctime(thread))
    print(calctime(muti_process))

在这里插入图片描述

注意事项:

  1. 要注意在开启线程的程序中,一定要注意变量的使用是否是赋值、浅拷贝、还是深拷贝,则会影响到线程之间的变量共享使用等!
  2. 在开启多进程时候,必须要指定主程序__name__ == ‘main
  3. 使用map是顺序输出,要想不顺序输出,可以使用as_completed
    在这里插入图片描述
    图片来源

多进程共享变量

#! /usr/bin/env python3
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
from multiprocessing import Lock, Manager

loc = Lock()

x = {'a':0,'b':1}

def Files_Parse(args):
    with loc():
        args[0][args[1]] += 1


# parse the data file


def parse_RF_files(dic):
    args = [(dic, 'a'), (dic, 'b')]
    with ProcessPoolExecutor() as pool:
        results = pool.map(Files_Parse, args)
    # for arg in args:
    #     Files_Parse(arg)


if __name__ == "__main__":
    m = Manager()
    dic = m.dict(x)
    for i in range(2):
        parse_RF_files(dic)
        print(dic)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一WILLPOWER一

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值