Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1778397
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-05-06 19:11:28


点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. #-*- coding:utf-8 -*-
  3. '''
  4. '''
  5. #多进程,pool
  6. from multiprocessing import Process
  7. from multiprocessing import Pool
  8. import os
  9. import time
  10. import random
  11. def f(name):
  12.     print('hello, %s,pid=%s' % (name, os.getpid()))
  13. if __name__ == '__main__':
  14.     print('Parent process %s ' % os.getpid())
  15.     p=Process(target=f, args=('talen',))
  16.     print('Child process will start.')
  17.     p.start()
  18.     p.join()
  19.     print('Child process end')
  20. def long_time_task(name):
  21.     print('Run task %s (%s)...' % (name,os.getpid()))
  22.     start=time.time()
  23.     time.sleep(random.random() * 3)
  24.     end=time.time()
  25.     print('Task %s runs %0.2f seconds' % (name,(end - start )))
  26. if __name__ == '__main__':
  27.     print('Parent process %s ' % os.getpid())
  28.     pp=Pool(4)
  29.     for i in range(6):
  30.         pp.apply_async(long_time_task,args=(i,))
  31.     print('Child process will start.')
  32.     pp.close()
  33.     pp.join()
  34.     print('Child process end')

  35. #子进程
  36. import subprocess
  37. print('$ nslookup htfchina.blog.chinaunix.net')
  38. r = subprocess.call(['nslookup','htfchina.blog.chinaunix.net'])
  39. print('Exit code :',r)

  40. #输入网址
  41. print('$ nslookup')
  42. subp=subprocess.Popen(['nslookup '], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  43. output, err = subp.communicate(b'set q=mx\n\nexit\n')
  44. print(output.decode('utf-8'))
  45. print('Exit code:',subp.returncode)


点击(此处)折叠或打开

  1. /usr/bin/python3 /home/t/PycharmProjects/untitled/mutliprocessing_t.py
  2. Parent process 14001
  3. Child process will start.
  4. hello, talen,pid=14002
  5. Child process end
  6. Parent process 14001
  7. Child process will start.
  8. Run task 0 (14003)...
  9. Run task 1 (14004)...
  10. Run task 2 (14005)...
  11. Run task 3 (14006)...
  12. Task 3 runs 0.02 seconds
  13. Run task 4 (14006)...
  14. Task 0 runs 2.07 seconds
  15. Run task 5 (14003)...
  16. Task 1 runs 2.46 seconds
  17. Task 4 runs 2.58 seconds
  18. Task 2 runs 2.97 seconds
  19. Task 5 runs 2.33 seconds
  20. Child process end
  21. $ nslookup htfchina.blog.chinaunix.net
  22. Server:        10.10.106.201
  23. Address:    10.10.106.201#53

  24. Non-authoritative answer:
  25. Name:    htfchina.blog.chinaunix.net
  26. Address: 61.55.167.140

  27. Exit code : 0
  28. $ nslookup
  29. Server:        10.10.106.201
  30. Address:    10.10.106.201#53

  31. Non-authoritative answer:
  32. www.baidu.com    canonical name = www.a.shifen.com.

  33. Authoritative answers can be found from:
  34. a.shifen.com
  35.     origin = ns1.a.shifen.com
  36.     mail addr = baidu_dns_master.baidu.com
  37.     serial = 1605030003
  38.     refresh = 5
  39.     retry = 5
  40.     expire = 86400
  41.     minimum = 3600


  42. Exit code: 0

  43. Process finished with exit code 0

阅读(1039) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~