首页 > 编程语言 >Python - 并行

Python - 并行

时间:2022-12-02 23:22:26浏览次数:32  
标签:10 39 23 Python 并行 doing result loiter

futures


# 使用方法一:
def display(*args):
    print(strftime('[%H:%M:%S]'), end=' ')
    print(*args)


def loiter(n):
    msg = '{}loiter({}): doing nothing for {}s...'
    display(msg.format('\t' * n, n, n))
    sleep(n)
    msg = '{}loiter({}): done'
    display(msg.format('\t' * n, n))
    return n * 10


def main():
    print('Script starting')
    with futures.ThreadPoolExecutor(max_workers=3) as executor:  # max_workers=3: 指定线程池中的线程数
        results =  executor.map(loiter, range(5))  # 将 0~4 传递给loiter函数,返回一个迭代器
        display('results:', results)
        for i, result in enumerate(results):  # 取loiter返回的每个结果
            display('result {}:{}'.format(i, result))

main()


# out:
'''
Script starting
[23:10:39] loiter(0): doing nothing for 0s...
[23:10:39] loiter(0): done
[23:10:39] 	loiter(1): doing nothing for 1s...
[23:10:39] 		loiter(2): doing nothing for 2s...
[23:10:39] 			loiter(3): doing nothing for 3s...[23:10:39]
 results: <generator object Executor.map.<locals>.result_iterator at 0x000001BC1D181A10>
[23:10:39] result 0:0
[23:10:40] 	loiter(1): done
[23:10:40] 				loiter(4): doing nothing for 4s...[23:10:40] result 1:10

[23:10:41] 		loiter(2): done
[23:10:41] result 2:20
[23:10:42] 			loiter(3): done
[23:10:42] result 3:30
[23:10:44] 				loiter(4): done
[23:10:44] result 4:40

Process finished with exit code 0

'''

标签:10,39,23,Python,并行,doing,result,loiter
From: https://www.cnblogs.com/czzz/p/16945983.html

相关文章

  • Python13章
    实验13:Pygame游戏编程一、实验目的和要求二、实验环境Python3.1064_bit三、实验过程1、实例1:制作一个跳跃的小球游戏(1)代码如下:1#-*-coding:utf-8-*-2......
  • securecrt9使用Python3
     很长一段时间,securecrt支持python,但版本为python2,而且自带的python解释器模块有缺失,关键是不支持三方库,使用上不方便。securecrt9.0开始支持Python3,不像python2.7......
  • 4-python的数据类型
    python为了应对不同的业务需求,也将数据分成了不同的类型1-numbersint(有符号整型)long(长整型可以代表八进制和16进制)float(浮点型)comple......
  • python中的公共操作与列表推导式
    1.公共操作#+合并将两个相同类型序列进行连接字符串、列表、元组l1=[1,2,3]l2=[4,5,6]print(l1+l2)#[1,2,3,4,5,6]  #*复制将里面的......
  • Python实验报告(第13章)
    实验13:Pygame游戏编程一、实验目的和要求学会Pygame的基本应用二、实验环境软件版本:Python3.1064_bit三、实验过程1、实例1:制作一个跳跃的小游戏(1)代码如下:1......
  • Python数据分析(一)--Matplotlib学习
    Matplotlib库学习2.1Matplotlib库小测2.2Pyplot的绘图区域plt.subplot(nrows,ncols,plot_number)图表的整个绘图区域被分成numRows行和numCols列然后按......
  • 【Python】【爬虫】爬取豆瓣电影评分
    importrequestsimportreimportcsvurl="https://movie.douban.com/top250"headers={"User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537......
  • Python数据分析(一)--Numpy学习
    Numpy学习1.数据的维度1.1一维数据一维数据由对等关系的有序或无序数据构成,采用线性方式组织。对应列表(有序)、数组和集合(无序)等类型。例子:列表和数组相同点:......
  • Python+NumPy绘制常见曲线的方法详解_python
    一、利萨茹曲线二、计算斐波那契数列 三、方波方波可以近似表示为多个正弦波的叠加。任意一个方波信号都可以用无穷傅里叶级数来表示。需要累加很多项级数,且级数越......
  • python生成中奖号码工具
    前言:不知道有没有人和我一样,学习python编程的时候有个想法,模似中奖程序,预测中奖号码。废话不多说,直接实操。一、红球1-32蓝球1-15二、中奖规则一等奖:6个红色球号码+1个蓝色......