Python并行程序_CodingPark编程公园

Python 并行处理

ProcessPoolExecutor()

多进程 - 针对 计算型 的数据处理

一般方法

在这里插入图片描述

结果显示
在这里插入图片描述

并行处理

# -*- encoding: utf-8 -*-
"""
@File    :   parallel.py
@Contact :   ag@team-ag.club
@License :   (C)Copyright 2019-2020, CodingPark

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2020-10-26 20:36   AG         1.0         None
"""
from multiprocessing.dummy import Pool
import concurrent.futures
from qutip import *
from pylab import *


def run(KA):
    w1 = wr = w2 = 6.0 * 2 * pi
    g1 = 0.05 * 2 * pi
    g2 = 0.05 / (sqrt(2) + 1) * 2 * pi
    KT = KA * 1e3
    Kappa = 1 / KT
    # the level of cavity
    N = 4
    # the annihlation of cavity |w1,wr,w2>
    ar = tensor(qeye(N), destroy(N), qeye(N))
    a1 = tensor(destroy(N), qeye(N), qeye(N))
    a2 = tensor(qeye(N), qeye(N), destroy(N))
    # the level of cavity
    c_op_list = []
    c_op_list.append(sqrt(Kappa) * ar)
    c_op_list.append(sqrt(Kappa) * a1)
    c_op_list.append(sqrt(Kappa) * a2)
    # the initial state of system
    psi0 = tensor(basis(N, 1), basis(N, 0), basis(N, 0))
    psi1 = tensor(basis(N, 0), basis(N, 0), basis(N, 1))
    psi2 = tensor(basis(N, 0), basis(N, 1), basis(N, 0))
    psf = 1 / sqrt(2) * (psi0 + psi1)
    #    #the evolution of time
    T = 15

    # the Haimoltonian of the system
    HI = g1 * (a1.dag() * ar + a1 * ar.dag()) + g2 * (a2.dag() * ar + a2 * ar.dag())

    # the evolution of the system
    tlist = linspace(0.0, T, T / 0.01 + 1)
    output = mesolve(HI, psi0, tlist, c_op_list, [psf * psf.dag()])
    # search the value number
    F = real(output.expect[0])
    F = list(F)
    po = F.index(max(F))
    print(KA, tlist[po], F[po])
    out = open('BellFidelity' + str(KA) + '.txt', 'a')
    for m in range(len(tlist)):
        string = str(tlist[m]) + ',' + str(F[m])
        out.write(string + '\n')

    out.close()
    # plot the picture
    plot(tlist, output.expect[0])
    legend(("BellFidelity"))
    xlabel('time')
    ylabel('probablity')
    title('qubit-resonator state swapping')
    ylim([0, 1])
    show()


if __name__ == '__main__':
    paras = list()
    for ka in range(1, 100, 1):
        paras.append(ka)

    t = time.time()
    with concurrent.futures.ProcessPoolExecutor() as executor:
        executor.map(run, paras)
    # pool = Pool()
    # # paras = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    # # for ka in range(1, 11, 1):
    # #     paras.append(ka)
    #
    # results = pool.map(run, [1, 2, 3])
    #
    # pool.close()
    # pool.join()

    print('over')
    print("Mission complete-Parallel, it took {:.3f}s".format(time.time() - t))


1 - 代码中,首先把具体的处理过程打包成函数 run( )

2 - 这句意味着你有多少CPU核心,就启动多少Python进程

with concurrent.futures.ProcessPoolExecutor() as executor:

3 - 读入原始数据

paras = list()
for ka in range(1, 100, 1):
paras.append(ka)

4 - 实际的处理语句,第一个参数是处理函数,第二个参数是原始数据

executor.map(run, paras)

结果显示
在这里插入图片描述

ThreadPoolExecutor()

多线程 - 针对 IO 型的数据处理

# -*- encoding: utf-8 -*-
"""
@File    :   test.py
@Contact :   ag@team-ag.club
@License :   (C)Copyright 2019-2020, CodingPark

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2020-10-25 09:46   AG         1.0        主程序+并行
"""
import os
from ocr import ocr
import time
import shutil
import numpy as np
import cv2
from PIL import Image
from glob import glob
import concurrent.futures


def single_pic_proc(image_file):
    image = np.array(Image.open(image_file).convert('RGB'))
    result, image_framed = ocr(image)
    return result, image_framed


def runIt(image_file):
    result_dir = './test_result'
    # t = time.time()
    result, image_framed = single_pic_proc(image_file)
    output_file = os.path.join(result_dir, image_file.split('/')[-1])
    txt_file = os.path.join(result_dir, image_file.split('/')[-1].split('.')[0] + '.txt')

    print(txt_file)
    txt_f = open(txt_file, 'w')
    Image.fromarray(image_framed).save(output_file)
    # print("Mission complete, it took {:.3f}s".format(time.time() - t))
    print("\nRecognition Result:\n")
    for key in result:
        print(result[key][1])
        txt_f.write(result[key][1] + '\n')
    txt_f.close()

def load_and_resize(image_filename):
    img = cv2.imread(image_filename)
    img = cv2.resize(img, (600, 600))
    print('===load_and_resize 运行完成===')



if __name__ == '__main__':
    t = time.time()

    with concurrent.futures.ThreadPoolExecutor() as executor:
        image_files = glob('./test_images/*.*')
        executor.map(runIt, image_files)

    print("\nMission complete, it took {:.3f}s".format(time.time() - t))

结果显示
在这里插入图片描述

更多参考

📍3行代码实现 Python 并行处理,速度提高6倍!
https://blog.csdn.net/u011808673/article/details/85341977
📍Python并行执行for循环
https://blog.csdn.net/qq_23869697/article/details/84798614?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-4-84798614.nonecase&utm_term=for循环并行&spm=1000.2123.3001.4430

在这里插入图片描述

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页
实付 79.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值