博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 多进程与子进程
阅读量:6943 次
发布时间:2019-06-27

本文共 2363 字,大约阅读时间需要 7 分钟。

点击(此处)折叠或打开

  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\nwww.baidu.com\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

转载地址:http://jzonl.baihongyu.com/

你可能感兴趣的文章
请求浏览器使用chrome查看http请求
查看>>
数据数据包JAVASE----17----网络编程
查看>>
OC基础知识
查看>>
matlab之并行
查看>>
泛型类型通常在Dao和Service 中使用BaseDao<T extends Serializable>的泛型
查看>>
关于java的JIT知识
查看>>
设备编程[置顶] Linux 设备编程
查看>>
冒泡、选择、插入排序
查看>>
SQL简单的日报和月报
查看>>
listView 分页加载数据
查看>>
【体系结构】转移预测器性能的定量评价
查看>>
[置顶] 浅谈软件质量管理
查看>>
Storm-源码分析-Topology Submit-Task
查看>>
POJ 2632 Crashing Robots (坑爹的模拟题)
查看>>
[转载]ubuntu发热问题解决
查看>>
__stdcall,__cdecl,_cdecl,_stdcall,。__fastcall,_fastcall 区别简介
查看>>
Chapter 1 -- UsingAndAvoidingNull
查看>>
HTTP 500 - 内部服务器错误
查看>>
马婕 2014年MBA,mpacc备考 报刊宣读2 美国对互联网的控制威胁着网络自由(转)
查看>>
Matlab之视角旋转函数[转]
查看>>