点击(此处)折叠或打开
-
#!/usr/bin/env python3
-
#-*- coding:utf-8 -*-
-
'''
-
'''
-
#多进程,pool
-
from multiprocessing import Process
-
from multiprocessing import Pool
-
import os
-
import time
-
import random
-
def f(name):
-
print('hello, %s,pid=%s' % (name, os.getpid()))
-
if __name__ == '__main__':
-
print('Parent process %s ' % os.getpid())
-
p=Process(target=f, args=('talen',))
-
print('Child process will start.')
-
p.start()
-
p.join()
-
print('Child process end')
-
def long_time_task(name):
-
print('Run task %s (%s)...' % (name,os.getpid()))
-
start=time.time()
-
time.sleep(random.random() * 3)
-
end=time.time()
-
print('Task %s runs %0.2f seconds' % (name,(end - start )))
-
if __name__ == '__main__':
-
print('Parent process %s ' % os.getpid())
-
pp=Pool(4)
-
for i in range(6):
-
pp.apply_async(long_time_task,args=(i,))
-
print('Child process will start.')
-
pp.close()
-
pp.join()
-
print('Child process end')
-
-
#子进程
-
import subprocess
-
print('$ nslookup htfchina.blog.chinaunix.net')
-
r = subprocess.call(['nslookup','htfchina.blog.chinaunix.net'])
-
print('Exit code :',r)
-
-
#输入网址
-
print('$ nslookup')
-
subp=subprocess.Popen(['nslookup '], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-
output, err = subp.communicate(b'set q=mx\nwww.baidu.com\nexit\n')
-
print(output.decode('utf-8'))
- print('Exit code:',subp.returncode)
点击(此处)折叠或打开
-
/usr/bin/python3 /home/t/PycharmProjects/untitled/mutliprocessing_t.py
-
Parent process 14001
-
Child process will start.
-
hello, talen,pid=14002
-
Child process end
-
Parent process 14001
-
Child process will start.
-
Run task 0 (14003)...
-
Run task 1 (14004)...
-
Run task 2 (14005)...
-
Run task 3 (14006)...
-
Task 3 runs 0.02 seconds
-
Run task 4 (14006)...
-
Task 0 runs 2.07 seconds
-
Run task 5 (14003)...
-
Task 1 runs 2.46 seconds
-
Task 4 runs 2.58 seconds
-
Task 2 runs 2.97 seconds
-
Task 5 runs 2.33 seconds
-
Child process end
-
$ nslookup htfchina.blog.chinaunix.net
-
Server: 10.10.106.201
-
Address: 10.10.106.201#53
-
-
Non-authoritative answer:
-
Name: htfchina.blog.chinaunix.net
-
Address: 61.55.167.140
-
-
Exit code : 0
-
$ nslookup
-
Server: 10.10.106.201
-
Address: 10.10.106.201#53
-
-
Non-authoritative answer:
-
www.baidu.com canonical name = www.a.shifen.com.
-
-
Authoritative answers can be found from:
-
a.shifen.com
-
origin = ns1.a.shifen.com
-
mail addr = baidu_dns_master.baidu.com
-
serial = 1605030003
-
refresh = 5
-
retry = 5
-
expire = 86400
-
minimum = 3600
-
-
-
Exit code: 0
-
- Process finished with exit code 0