1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/usr/bin/env python # coding=utf-8 from multiprocessing import Pool import os,time,random import subprocess def long_time_task(name): webiste = ['www.baidu.com','www.youku.com','www.tudou.com','www.iqiyi.com','www.google.com','www.503error.com'] print '---------Run task %s (%s)...' % (name,os.getpid()) start = time.time() time.sleep(2) print name print webiste[name] subprocess.call(['ping','-c','1',webiste[name-1]]) #print 'Result:',r end = time.time() print '*********Task %s run %0.2f second' % (name,end-start) if __name__ =='__main__': print 'Parent process %s',os.getpid() p = Pool(4) for i in range(5): p.apply_async(long_time_task, args=(i,)) print 'Waiting all the process' p.close() p.join() print 'All done' |
[zhizhang@zhizhang-desktop-nay test]$ vi myProcessPoll.py
[zhizhang@zhizhang-desktop-nay test]$ cat myProcessPoll.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/usr/bin/env python # coding=utf-8 from multiprocessing import Pool import os,time,random import subprocess def long_time_task(name): webiste = ['www.baidu.com','www.youku.com','www.tudou.com','www.iqiyi.com','www.google.com','www.503error.com'] print '---------Run task %s (%s)...' % (name,os.getpid()) start = time.time() time.sleep(2) print name print webiste[name] subprocess.call(['ping','-c','1',webiste[name-1]]) #print 'Result:',r end = time.time() print '*********Task %s run %0.2f second' % (name,end-start) if __name__ =='__main__': print 'Parent process %s',os.getpid() p = Pool(4) for i in range(5): p.apply_async(long_time_task, args=(i,)) print 'Waiting all the process' p.close() p.join() print 'All done' |
Latest posts by Zhiming Zhang (see all)
- 什么是ami - 二月 22, 2021
- istio Ingress Gateways - 十一月 25, 2020
- Istio VirtualService - 十一月 23, 2020