1.yield协成:
# 本来想使用协成但是发现效果差不多
def get_coroutine(f):
def wrapper(*args, **kwargs):
gen = f(*args) # retfunc
r = gen.__next__() # 激活生成器函数delete_docker
def inner(g):
try:
g.__next__() # 调用delete_docker.__next__(),去执行耗时操作
print("完成IO操作----------------------")
# gen.send(ret) # 如果有需求可以send值
except StopIteration:
pass
# 产生一个线程去执行,耗时操作
threading.Thread(target=inner, args=(r,)).start()
# 执行retfunc.__next__()先一步将taskFinally的结果返回给前端
response = gen.__next__()
return response
return wrapper
@get_coroutine
def retfunc(info, D, linux):
ret = taskFinally(info, D)
yield delete_docker(info, linux)
yield ret
def taskFinally():
pass
def delete_docker(info, linux):
"""删除docker容器"""
try:
linux = linux
linux.LinuxSend('docker rm -f %s' % info['dockername'])
if re.search(info['dockername'], linux.LinuxSend('docker ps -a')):
print('docker删除失败')
else:
global ip_list
ip_list.remove(info["dockerip"])
print('docker删除成功', info["dockerip"])
linux.LinuxClose()
except:
linux.LinuxClose()
print('docker删除失败')
finally:
yield