Python多进程多线程测试

今天在工作中遇到爬虫效率问题,在此处记录多进程、多线程测试脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Seven'
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time


def gcd(pair):
    a, b = pair
    low = min(a, b)
    for i in range(low, 0, -1):
        if a % i == 0 and b % i == 0:
            return i


numbers = [
    (1963309, 2265973), (1879675, 2493670), (2030677, 3814172),
    (1551645, 2229620), (1988912, 4736670), (2198964, 7876293)
]


def thread_map_test():
    start_time = time.time()
    with ThreadPoolExecutor(max_workers=4) as pool:
        results = pool.map(gcd, numbers)
    results = list(results)
    end_time = time.time()
    print(f'运行结果:{results}')
    print(f'多线程map运行时长:{end_time - start_time}')


def thread_submit_test():
    start_time = time.time()
    results = []
    with ThreadPoolExecutor(max_workers=4) as pool:
        for i in numbers:
            future = pool.submit(gcd, i)
            results.append(future)
    results = [result.result() for result in results]
    end_time = time.time()
    print(f'运行结果:{results}')
    print(f'多线程submit运行时长:{end_time - start_time}')


def process_map_test():
    start_time = time.time()
    with ProcessPoolExecutor(max_workers=4) as pool:
        results = pool.map(gcd, numbers)
    results = list(results)
    end_time = time.time()
    print(f'运行结果:{results}')
    print(f'多进程map运行时长:{end_time - start_time}')


def process_submit_test():
    start_time = time.time()
    results = []
    with ProcessPoolExecutor(max_workers=4) as pool:
        for i in numbers:
            future = pool.submit(gcd, i)
            results.append(future)
    results = [result.result() for result in results]
    end_time = time.time()
    print(f'运行结果:{results}')
    print(f'多进程submit运行时长:{end_time - start_time}')


if __name__ == '__main__':
    thread_map_test()
    thread_submit_test()
    process_map_test()
    process_submit_test()

 

当多进程/多线程传参时,一个为可变变量,一个为不可变变量,可参照如下代码进行传参:

with ProcessPoolExecutor(max_workers=4) as pool:
    pool.map(partial(data_crawl, variable_constant=variable_constant), variable_changed)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Seven°

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

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

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

打赏作者

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

抵扣说明:

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

余额充值