目标
用一个等待过程理解,能够实现一致的方便,70行代码
例子说明
某方需要2,执行req2需要执行这个时间,最终执行时间是3秒,大约1秒,同时执行;如果最终时间是秒,如果执行的话,具体请参看,友情提示是,yield from 方法后面如果是它的工具,可以进入____
import time
from collections import deque
_delay = deque()
class FutureX:
def __init__(self, coro=None, delay_second=None):
self.coro = coro
if delay_second:
self.start = delay_second + time.time()
def step(self):
coro = self.coro
try:
result = coro.send(None)
except StopIteration as e:
print(e.value)
pass
else:
if isinstance(result, FutureX):
_delay.append((self._wakeup, result))
else:
pass
def _wakeup(self):
self.step()
def __iter__(self):
yield self
return None
def coroutine(func):
co = func.__code__
func.__code__ = co.replace(co_flags=co.co_flags | 0x100)
return func
@coroutine
def sleep0(seconds):
future = FutureX(delay_second=seconds)
b = yield from future
return seconds
async def req1(delay_seconds):
resp_time = await sleep0(delay_seconds)
return resp_time
async def req2(delay_seconds):
resp_time = await sleep0(delay_seconds)
return resp_time
t1 = time.time()
f1, f2 = FutureX(req1(2)), FutureX(req2(1))
f1.step()
f2.step()
while _delay:
callback, args = _delay.popleft()
start = args.start
if not start:
continue
while True:
end = time.time()
if start <= end:
try:
callback()
except StopIteration as e:
pass
break
print(f'花费的时间:{round(time.time() - t1,1)}')
'''
结果:
2
1
花费的时间:2.0
'''
审核编辑:刘清
-
python
+关注
关注
56文章
4781浏览量
84443
发布评论请先 登录
相关推荐
评论