zoukankan      html  css  js  c++  java
  • 多进程复习

    # import os
    # #fork只能用于linux/unix中
    # pid = os.fork()
    # print("bobby")
    # if pid == 0:
    #   print('子进程 {} ,父进程是: {}.' .format(os.getpid(), os.getppid()))
    # else:
    #   print('我是父进程:{}.'.format(pid))
    
    
    import multiprocessing
    
    #多进程编程
    import time
    def get_html(n):
        time.sleep(n)
        print("sub_progress success")
        return n
    
    
    if __name__ == "__main__":
        # progress = multiprocessing.Process(target=get_html, args=(2,))
        # print(progress.pid)
        # progress.start()
        # print(progress.pid)
        # progress.join()
        # print("main progress end")
    
        #使用线程池
        pool = multiprocessing.Pool(multiprocessing.cpu_count())
        # result = pool.apply_async(get_html, args=(3,))
        #
        # #等待所有任务完成
        # pool.close()
        # pool.join()
        #
        # print(result.get())
    
        #imap
        # for result in pool.imap(get_html, [1,5,3]):
        #     print("{} sleep success".format(result))
    
        for result in pool.imap_unordered(get_html, [1,5,3]):
            print("{} sleep success".format(result))
  • 相关阅读:
    缓存清理
    机器学习在电商领域三大应用,推荐,搜索,广告中商品排序
    并发和并行
    拷贝控制
    gitk
    git GUI Clients
    new delete
    Windows 安装 gcc
    C++ 运算符优先级
    iostream 操作符
  • 原文地址:https://www.cnblogs.com/Erick-L/p/8922417.html
Copyright © 2011-2022 走看看