免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3141 | 回复: 1
打印 上一主题 下一主题

python多进程Multiprocessing使用问题 [复制链接]

论坛徽章:
1
水瓶座
日期:2014-12-25 11:15:10
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-01-28 11:02 |显示全部楼层 |倒序浏览
代码:
  1. # /usr/bin/python
  2. #coding:utf-8

  3. import multiprocessing,os,re,time

  4. begintime = time.time()
  5. def mtlogGzip(gzipfile):
  6.     #print 'ddd'
  7.     os.popen('tar -zcf %s.gz %s' %(gzipfile,gzipfile)).readline()

  8. def muti_gzip():
  9.     pool = multiprocessing.Pool(processes=4)
  10.     for i in os.listdir('.'):
  11.         if re.match(r'^20',i):
  12.             print i
  13.             pool.apply_async(mtlogGzip(i,))
  14.     pool.close()
  15.     pool.join()


  16. if __name__=='__main__':
  17.     muti_gzip()
  18. endtime = time.time()
  19. usetime =  endtime - begintime
  20. print "执行脚本总用时 %s 秒" % usetime
复制代码
执行后,ps,跟top看到的内容

ps
  1. [root@localhost ~]# ps aux | grep test_gzip_02.py
  2. root     16772  0.0  0.0 334716  7204 pts/0    Sl+  10:30   0:00 python test_gzip_02.py
  3. root     16773  0.0  0.0 183148  4928 pts/0    S+   10:30   0:00 python test_gzip_02.py
  4. root     16774  0.0  0.0 183148  4816 pts/0    S+   10:30   0:00 python test_gzip_02.py
  5. root     16775  0.0  0.0 183148  4816 pts/0    S+   10:30   0:00 python test_gzip_02.py
  6. root     16776  0.0  0.0 183148  4820 pts/0    S+   10:30   0:00 python test_gzip_02.py
  7. root     16884  0.0  0.0 103248   872 pts/1    S+   10:35   0:00 grep test_gzip_02.py
复制代码
top
  1. 17108 root      20   0  4296  676  308 S  4.3  0.0   0:25.13 gzip                                                                                                                     
  2. 17107 root      20   0  113m 1184  988 D  3.3  0.0   0:07.62 tar                                                                                                                                                                                                                                            
  3.     1 root      20   0 19228  252  104 S  0.0  0.0   0:03.54 init                                                                                                                     
  4.     2 root      20   0     0    0    0 S  0.0  0.0   0:11.86 kthreadd                                                                                                                  
  5.     3 root      RT   0     0    0    0 S  0.0  0.0   0:24.89 migration/0                                                                                                               
  6.     4 root      20   0     0    0    0 S  0.0  0.0   0:18.39 ksoftirqd/0
复制代码
可以看到top里面只有一个tar,一个gzip在执行操作,并没有多个同时进行压缩操作,这是为什么的?

论坛徽章:
1
水瓶座
日期:2014-12-25 11:15:10
2 [报告]
发表于 2016-01-29 11:10 |显示全部楼层
本帖最后由 水底游 于 2016-01-29 11:10 编辑

已经解决了

apply_async(func[, args[, kwds[, callback]]])
A variant of the apply() method which returns a result object.

以上是apply_async的函数原型,参数传递有问题,apply_async的第一个参数是函数对象,第二个参数是函数对象的参数,你把参数也写到第一个参数里,相当于调用函数,把函数的执行结果当第一个参数传递给了apply_async,所以等于是同步执行,正确的姿势用如下方式调用:

pool.apply_async(mtlogGzip,(i,))

回复 2# zhonghua7896321


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP