#!/bin/env python
#coding:utf-8
import time,re
import os,datetime
from concurrent import futures
data = [‘1‘,‘2‘]
def wait_on(argument):
print argument
time.sleep(2)
return ‘ok‘
ex = futures.ThreadPoolExecutor(max_workers=2)
for i in ex.map(wait_on,data):
print i
map函数异步执行完成之后,结果也是list,数据需要从list中取出
######################################
submit函数和map函数,根据需要,选一个使用即可。
shutdown(wait=True)
此函数用于释放异步执行操作后的系统资源。
If wait is True then this method will not return until all the
pending futures are done executing and the resources associated with the
executor have been freed. If wait is False then this method will return
immediately and the resources associated with the executor will be
freed when all pending futures are done executing. Regardless of the
value of wait, the entire Python program will not exit until all pending
futures are done executing.
You
can avoid having to call this method explicitly if you use the with
statement, which will shutdown the Executor (waiting as if
Executor.shutdown() were called with wait set to True):
with ThreadPoolExecutor(max_workers=4) as e:
e.submit(shutil.copy, ‘src1.txt‘, ‘dest1.txt‘)
三,完整的concurrent例子:
#!/bin/env python
#coding:utf-8
import time,re,fcntl
import os,datetime
from concurrent import futures
count_list = list()
MinuteNum = 1
StartTime = datetime.datetime(2014, 4, 16, 19, 31, 0, 484870)
NowTime = datetime.datetime.now()
os.system(‘:>new.txt‘)
f_new = open(‘new.txt‘,‘a‘)
def test(CountTimeFormat):
f = open(‘push_slave.stdout‘,‘r‘)
for line in f.readlines():
if re.search(CountTimeFormat,line):