python 多进程与子进程

简介: 点击(此处)折叠或打开 #!/usr/bin/env python3 #-*- coding:utf-8 -*- ''' ''' ...

点击(此处)折叠或打开

  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

目录
相关文章
|
6天前
|
监控 Python
python过滤指定进程
python过滤指定进程
13 1
|
6天前
|
运维 监控 Ubuntu
Python实现ubuntu系统进程内存监控
Python实现ubuntu系统进程内存监控
12 1
|
6天前
|
开发者 Python
在Python中查询进程信息的实用指南
在Python中查询进程信息的实用指南
9 2
|
14天前
|
消息中间件 Linux 调度
Python的进程锁,进程队列
Python的进程锁,进程队列
122 3
|
14天前
|
数据采集 监控 调度
Python的进程,以及进程同步,守护进程详细解读
Python的进程,以及进程同步,守护进程详细解读
138 4
|
14天前
|
调度 Python 容器
【python】-详解进程与线程
【python】-详解进程与线程
|
18天前
|
运维 监控 Unix
第十五章 Python多进程与多线程
第十五章 Python多进程与多线程
|
19天前
|
Java 数据库连接 数据处理
Python从入门到精通:3.1.2多线程与多进程编程
Python从入门到精通:3.1.2多线程与多进程编程
|
19天前
|
消息中间件 安全 调度
Python从入门到精通:3.1.1多线程与多进程——进程和线程的概念
Python从入门到精通:3.1.1多线程与多进程——进程和线程的概念
|
25天前
|
调度 Python
Python多线程、多进程与协程面试题解析
【4月更文挑战第14天】Python并发编程涉及多线程、多进程和协程。面试中,对这些概念的理解和应用是评估候选人的重要标准。本文介绍了它们的基础知识、常见问题和应对策略。多线程在同一进程中并发执行,多进程通过进程间通信实现并发,协程则使用`asyncio`进行轻量级线程控制。面试常遇到的问题包括并发并行混淆、GIL影响多线程性能、进程间通信不当和协程异步IO理解不清。要掌握并发模型,需明确其适用场景,理解GIL、进程间通信和协程调度机制。
35 0