python多程式與子程式

天飛.h發表於2016-05-06

點選(此處)摺疊或開啟

  1. #!/usr/bin/env python3
  2. #* coding:utf8 *
  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
    www.baidu.com
    exit
    `
    )
  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. Nonauthoritative 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. Nonauthoritative 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


相關文章