zoukankan      html  css  js  c++  java
  • python多线程与多进程


    之前一直很不理解几个问题:
    1. 为什么说python多线程只是表面上的多线程?
    2. 多线程是真正的并发执行吗?
    3. 多进程和多线程有什么区别?

    今天有空,做了一个实验,算是搞懂了python中串行,多线程,多进程之间的区别。

    • 主机配置:

      • 6核cpu
    • 实验脚本:

      import threading
      from time import time
      from multiprocessing import cpu_count
      from multiprocessing import Process
      <>
      def loop(k):
      n=0
      for i in range(1000000):
      n+=i
      print k,
      return
      <>
      if name == "main":
      <#串行>
      count = 1
      seri_start = time()
      print "serial start at: " + str(seri_start)
      for i in range(count):
      loop(i)
      print " serial cost: " + str(time() - seri_start)
      <多线程执行>
      para_start = time()
      print "parallel start at: " +str(para_start)
      theads = []
      for i in range(count):
      t = threading.Thread(target=loop,args=(i,))
      theads.append(t)
      for thead in theads:
      thead.start()
      for thead in theads:
      thead.join()
      print " parallel cost: " + str(time() - para_start)
      <多进程执行>
      ncpu = cpu_count()
      process = []
      mp_start = time()
      print "multiprocessing start at: " + str(mp_start)
      for i in range(count):
      process.append(Process(target=loop,args=(i,)))
      for p in process:
      p.start()
      for p in process:
      p.join()
      print " multiprocessing cost: " + str(time() - mp_start)

      
      
    • count = 1

      serial start at: 1536741999.16
      0
      serial cost: 0.0729999542236
      parallel start at: 1536741999.24
      0
      parallel cost: 0.0650000572205
      multiprocessing start at: 1536741999.3
      0
      multiprocessing cost: 0.153000116348

      - 函数体执行一次所需时间为0.07s(以串行为标准,因为多线程或者多进程可能涉及其他的资源调度等问题)
      
      
    • count = 10

    serial start at: 1536742585.75
    0 1 2 3 4 5 6 7 8 9
    serial cost: 0.652000188828
    parallel start at: 1536742586.4
    01 2 3 4 5 6 78 9
    parallel cost: 1.4929997921
    multiprocessing start at: 1536742587.89
    5062143879
    multiprocessing cost: 0.290000200272

    	- 函数体执行10次
    		- 串行花费时间0.65s,完全按顺序执行函数体
    		- 多线程耗时1.49s,基本按顺序执行函数体
    		- 多进程耗时0.29s,完全无序执行函数体
    
    - count = 100
    	- ```
    serial start at: 1536742934.25 
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    serial cost: 6.44799995422
    parallel start at: 1536742940.7
    0 1 2 3 4 5 6 7 8 9 10 11 1312 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 4748 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 89 88 90 91 94 92 93 9695 9798 99
    parallel cost: 16.1419999599
    multiprocessing start at: 1536742956.84
    6193251175582427849018242510822201414817413528154434522157384083471346513398948050681656268175954236530714255828789999374643962927737767029369685434953663869458863679761609506931799173787212
    multiprocessing cost: 2.57500004768
    
    - 函数体执行100次
    	- 串行耗时6.44s,完全按顺序执行函数体
    	- 多线程耗时16.14s,基本按顺序执行函数体
    	- 多进程耗时2.57s,完全无序执行函数体
    
    • 数据总结:从以上3组数据可以看出

      • 串行耗时基本按倍数增长
      • 多线程耗时高于串行耗时
      • 多进程耗时小于串行耗时
    • 原因总结:

      • python多线程受限于GIL,所有并发的线程仍然在一个cpu上交替执行,虽然表面上是多个线程同时执行,实际上任一时间点只有一个线程被执行
      • 多线程耗时多于串行很多,可能是由于线程切换的耗时比较多(未考证), 所以对于不存在阻塞的线程, 使用python多线程意义不大
      • 使用multiprocessing的多进程处理,可以将每个进程均分到主机的各个cpu上执行(测试主机为6核cpu),所以多进程耗时较短,且进程之间完全无序执行
    • 因此,只有当线程中会出现阻塞时,多线程才有意义,比如线程中有数据下载,在等待数据返回时线程阻塞了,此时CPU就可以来处理其它线程的运算

    参考链接http://www.redicecn.com/html/Python/20111223/355.html

  • 相关阅读:
    腾讯//全排列
    腾讯//全排列
    腾讯//子集
    腾讯//子集
    腾讯///括号生成
    腾讯///括号生成
    腾讯//二叉树的最近公共祖先
    腾讯//二叉树的最近公共祖先
    腾讯//二叉搜索树的最近公共祖先
    腾讯//二叉搜索树的最近公共祖先
  • 原文地址:https://www.cnblogs.com/liushi-Oscar/p/9636051.html
Copyright © 2011-2022 走看看