zoukankan      html  css  js  c++  java
  • python 进程池

    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """
    @version: 001
    @author: jianpan
    @file: test1.py
    @time: 2017/6/4 16:14
    """
    
    # coding: utf-8
    import multiprocessing
    import time
    import os
    import psutil
    def func(msg):
        print "msg:", msg
        # print os.getgid()
        time.sleep(3)
        print "end"
    
    
    if __name__ == "__main__":
        pool = multiprocessing.Pool(processes=3)
        print os.getpid()
        for i in xrange(3):
            msg = "hello %d" % (i)
            pool.apply_async(func, (msg,))  # 维持执行的进程总数为processes,当一个进程执行完毕后会添加新的进程进去
    
        print "Mark~ Mark~ Mark~~~~~~~~~~~~~~~~~~~~~~"
        t = psutil.Process()
    
        pool.close()
        children = t.children()
        for child in children:
            print('Child pid is {}'.format(child.pid))
        pool.join()  # 调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
        print "Sub-process(es) done."
    
    
  • 相关阅读:
    希尔排序之C++实现(初级版)
    CF9D How many trees?
    IOI2015 boxes纪念品盒
    CSP-S 2019图论总结
    数据生成器
    Special-Judge模板
    CF293B Distinct Paths
    浅谈几种常见的剪枝方式
    CF620E New Year Tree
    浅谈DFS序
  • 原文地址:https://www.cnblogs.com/jian-pan/p/6941205.html
Copyright © 2011-2022 走看看