zoukankan      html  css  js  c++  java
  • 【10.9】multiprocessing多进程编程

     1 #!/user/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 import os
     5 import time
     6 # fork只能用于linux/unix中
     7 pid = os.fork()
     8 print('zy')
     9 if pid == 0:
    10     # os.getpid()当前进程的id,os.getppid()当前进程的父进程的id
    11     # 子进程会把父进程所有的数据拷贝一份,fork()之后的代码会重新运行一遍
    12     print('子进程 {} , 父进程是:{} .'.format(os.getpid(), os.getppid()))
    13 else:
    14     print('我是父进程:{}'.format(pid))
    15 
    16 # 父进程运行完成就退出,子进程不会退出
    17 time.sleep(2)
     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 import multiprocessing
     5 import time
     6 
     7 
     8 # 多进程编程
     9 def get_html(n):
    10     time.sleep(n)
    11     print('sub progress success')
    12     return n
    13 
    14 
    15 if __name__ == '__main__':
    16     progress = multiprocessing.Process(target=get_html, args=(2,))
    17     print(progress.pid)
    18     progress.start()
    19     print(progress.pid)
    20     progress.join()
    21     print('main progress end')
    22 
    23     # 使用进程池
    24     pool = multiprocessing.Pool(multiprocessing.cpu_count())
    25     # 提交任务
    26     result = pool.apply_async(get_html, args=(3,))
    27 
    28     # 关闭pool,不再接收新的任务
    29     pool.close()
    30     # 等待所有任务完成
    31     pool.join()
    32     print(result.get())
    None
    12052
    sub progress success
    main progress end
    sub progress success
    3
    

    imap

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 import multiprocessing
     5 import time
     6 
     7 
     8 # 多进程编程
     9 def get_html(n):
    10     time.sleep(n)
    11     print('sub progress success')
    12     return n
    13 
    14 
    15 if __name__ == '__main__':
    16     # 使用进程池
    17     pool = multiprocessing.Pool(multiprocessing.cpu_count())
    18 
    19     # imap,按照顺序打印
    20     for result in pool.imap(get_html, [1, 5, 3]):
    21         print('{} sleep suceess'.format(result))
    sub progress success
    1 sleep suceess
    sub progress success
    sub progress success
    5 sleep suceess
    3 sleep suceess
    

    imap_unordered

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 import multiprocessing
     5 import time
     6 
     7 
     8 # 多进程编程
     9 def get_html(n):
    10     time.sleep(n)
    11     print('sub progress success')
    12     return n
    13 
    14 
    15 if __name__ == '__main__':
    16     # 使用进程池
    17     pool = multiprocessing.Pool(multiprocessing.cpu_count())
    18 
    19     # imap,哪个先执行完打印哪个
    20     for result in pool.imap_unordered(get_html, [1, 5, 3]):
    21         print('{} sleep suceess'.format(result))
    sub progress success
    1 sleep suceess
    sub progress success
    3 sleep suceess
    sub progress success
    5 sleep suceess
    
  • 相关阅读:
    使用math.js进行javascript精确计算
    SpringMVC java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name
    MAC系统下配置环境变量
    javaURL中文乱码的问题
    Mac系统搭建java开发环境
    mysql 删除重复数据保留只保留一条
    SSM 加载配置文件
    CSS3实现元素旋转
    CSS3实现圆角效果
    CSS3 -web-box-shadow实现阴影效果
  • 原文地址:https://www.cnblogs.com/zydeboke/p/11309809.html
Copyright © 2011-2022 走看看