python 并发执行
来源:互联网 发布:冰与火之歌第七季 知乎 编辑:程序博客网 时间:2024/06/05 05:01
(1)需要执行python 程序如下:
#!/usr/bin/env pythonimport os,sysimport threadingfrom time import sleep,ctimeIpFileName='iplist.txt'shellFileName='shell.sh'iplist=[] class ThreadFunc(object): def __init__(self,func,args,name=''): self.name=name self.func=func self.args=args def __call__(self): self.res=self.func(*self.args)def loop(i,ip): cmd='sh ' + shellFileName + ' ' + ip os.system(cmd)def getIpList(): path=os.getcwd() if os.path.isfile(path+'/'+IpFileName)==False: print 'file ' + path+'/'+IpFileName + ' does not exist.' fd=open(path+'/'+IpFileName) content=fd.read().split('\n') fd.close() iplist=[] for ip in content: if ip!='': iplist.append(ip) return iplistdef main(): print 'starting at:',ctime() threads=[] iplist=getIpList() nloops=xrange(len(iplist)) print iplist for i in nloops: t=threading.Thread(target=ThreadFunc(loop,(i,iplist[i]),loop.__name__)) threads.append(t) for i in nloops: threads[i].start() for i in nloops: threads[i].join() print 'all done at:',ctime() if __name__=='__main__': main()
(2)python 中需要处理的文本文件iplist如下:
[wln@localhost concurrence]$ cat iplist.txt 10.40.127.110.40.127.210.40.127.310.40.127.410.40.127.510.40.127.6
(3)python程序中调用的shell脚本内容如下:
[wln@localhost concurrence]$ cat shell.sh echo $1
(4)执行python 程序结果如下:
[wln@localhost concurrence]$ python 07.py starting at: Mon Jul 7 03:05:41 2014['10.40.127.1', '10.40.127.2', '10.40.127.3', '10.40.127.4', '10.40.127.5', '10.40.127.6']10.40.127.310.40.127.210.40.127.410.40.127.510.40.127.610.40.127.1all done at: Mon Jul 7 03:05:41 2014
参考:
(1)python学习笔记——多线程编程
(2)python多线程学习
(3) python的threading和multiprocessing模块初探
0 0
- python 并发执行
- Python并发执行
- Python concurent Execution(Python 并发执行)
- python多线程并发执行程序实战讲解
- Python 中的串行执行 并发执行 同步异步
- 并发执行
- 基于python多线程实现Linux任务并发执行
- python里创建多个协程并发执行
- python里并发执行协程时部分阻塞超时怎么办
- Python 并发
- python 并发
- python并发
- AsyncTask执行顺序,并发执行。
- Java并发--任务执行
- 程序的并发执行
- Java并发--任务执行
- Java并发--任务执行
- web并发访问执行
- 栈 与 堆 的区别及其探讨
- 学生管理系统——修改部分
- perl语法总结
- jQuery 遍历 - 同胞
- 菜鸟进军c++(一)
- python 并发执行
- Heritage(高精度)
- 杭电Let the Balloon Rise
- oracle 通过with字句更新另一个表
- Android聊天表情开发
- 测试
- 情殷鉴古是什么意思
- Android Trick: 改变一个apk的packagename的简单流程,就2个步骤
- [HDU1158]DP 工厂雇佣决策