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

目录
相关文章
|
9天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
1月前
|
并行计算 安全 Unix
Python教程第8章 | 线程与进程
本章主要讲解了线程与进程的概念,多线程的运用以及Python进程的相关案例学习
36 0
|
1月前
|
分布式计算 并行计算 Java
浅析Python自带的线程池和进程池
浅析Python自带的线程池和进程池
79 0
|
26天前
|
安全 Python
Python中的并发编程:多线程与多进程技术探究
本文将深入探讨Python中的并发编程技术,重点介绍多线程和多进程两种并发处理方式的原理、应用场景及优缺点,并结合实例分析如何在Python中实现并发编程,以提高程序的性能和效率。
|
2天前
|
调度 Python
Python多线程、多进程与协程面试题解析
【4月更文挑战第14天】Python并发编程涉及多线程、多进程和协程。面试中,对这些概念的理解和应用是评估候选人的重要标准。本文介绍了它们的基础知识、常见问题和应对策略。多线程在同一进程中并发执行,多进程通过进程间通信实现并发,协程则使用`asyncio`进行轻量级线程控制。面试常遇到的问题包括并发并行混淆、GIL影响多线程性能、进程间通信不当和协程异步IO理解不清。要掌握并发模型,需明确其适用场景,理解GIL、进程间通信和协程调度机制。
18 0
|
26天前
|
并行计算 Python
Python中的并发编程:多线程与多进程的比较
在Python编程中,实现并发操作是提升程序性能的重要手段之一。本文将探讨Python中的多线程与多进程两种并发编程方式的优劣及适用场景,帮助读者更好地选择合适的方法来提高程序运行效率。
|
29天前
|
消息中间件 网络协议 API
Python语言的进程通讯及网络
Python语言的进程通讯及网络
|
1月前
|
安全 程序员 数据处理
深入探索Python多进程编程:理论与实践
深入探索Python多进程编程:理论与实践
36 2
|
5月前
|
Java 调度 Python
【Python】进程和线程
【Python】进程和线程
34 0
|
5月前
|
调度 Python
121 python高级 - 进程和线程
121 python高级 - 进程和线程
13 0