码迷,mamicode.com
首页 > 其他好文 > 详细

concurrent

时间:2018-05-04 01:39:08      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:ror   app   OLE   utf-8   pre   sub   form   googl   command   

#--coding:utf-8--
import os,sys
def excutecmd(cmd):
print(‘cmd:::{}‘.format(cmd))
try:
print(‘1command is {} and pid is {}‘.format(cmd, os.getpid()))
ret = os.system(cmd)
print(‘2command is {} and pid is {}‘.format(cmd,os.getpid()))
if ret ==0:
# os._exit(0)
# print(‘ccc‘)
return ret
else:
# os._exit(1)
# print(‘bbb‘)
return ret
except Exception as e:
# os._exit(2)
# print(‘aaa‘)
return e

def multithreadExcute(cmdlist = [],defaultprocessnum = 3):
result = []
from concurrent import futures
with futures.ThreadPoolExecutor(max_workers=defaultprocessnum) as excutor:
for cmd in cmdlist:
ret = excutor.submit(excutecmd,cmd)
result.append(ret.result())
return result

def printline(result):
print(‘printline::{}‘.format(result))
pass

def multiprocessExcute(cmdlist = [],defaultprocessnum = 2):
result = []
import multiprocessing
pool = multiprocessing.Pool(processes=2)
for cmd in cmdlist:
ret = pool.apply_async(func=excutecmd,args=(cmd,),callback=printline,error_callback=printline)
result.append(ret)
pool.close()
pool.join()
return [i.get() for i in result]

if __name__ == ‘__main__‘:
cmdlist = [‘ping www.ifeng.com‘,‘ping www.google.com‘,‘ping www.huawei.com‘]
ret = multiprocessExcute(cmdlist)
print(ret)
print(‘done!‘)

concurrent

标签:ror   app   OLE   utf-8   pre   sub   form   googl   command   

原文地址:https://www.cnblogs.com/billhsu2009/p/8988136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!