zoukankan      html  css  js  c++  java
  • 初探进程池线程池

    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/19 10:20
    # @File    : 进程池线程池.py

    # ProcessPoolExecutor和ThreadPoolExecutor接口一样
    # 计算密集型--进程  ,io密集型--线程
    # from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
    # import os,time,random
    #
    #
    # def task(name):
    #     print('name : %s pid: %s run' % (name, os.getpid()))
    #     time.sleep(random.randint(1, 3))  # 模拟运行时间
    #
    #
    # if __name__ == '__main__':
    #     pool = ProcessPoolExecutor(4)  # 如果不指定,默认就是cpu核数
    #     for i in range(10):
    #         pool.submit(task, 'egon%s' % i)  # 异步调用,提交完步,不用等,直接运行下面代码
    #
    #     pool.shutdown()  # 提交任务的入口关闭,默认参数wait=True,没有这句代码先打印主,有最后打印
    #
    #     print('主')


    from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
    from threading import currentThread
    import os,time,random


    def task():
        print('name : %s pid: %s run' % (currentThread().getName(), os.getpid()))
        time.sleep(random.randint(1, 3))  # 模拟运行时间


    if __name__ == '__main__':
        pool = ThreadPoolExecutor(5)  # 如果不指定,默认就是cpu核数
        for i in range(10):
            pool.submit(task, )

        pool.shutdown()

        print('主')


  • 相关阅读:
    Django 【第十二篇】Form组件进阶
    Django 【第十一篇】Form组件基础
    前端知识点总结
    vue组件续和前端工程化
    vue组件
    Vue视图下
    Vue实例与渲染
    BootStrap
    jQuery事件与动画
    dom操作 属性操作 样式操作
  • 原文地址:https://www.cnblogs.com/fmgao-technology/p/9197649.html
Copyright © 2011-2022 走看看