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

yield学习笔记

时间:2018-04-07 22:58:02      阅读:370      评论:0      收藏:0      [点我收藏+]

标签:val   ini   worker   image   return   inf   img   hand   after   

参考:http://www.dabeaz.com/finalgenerator/

from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(max_workers=8)

def func(x, y):
    time.sleep(5)
    return x, y

def do_func(x, y):
    result = yield pool.submit(func, x, y)
    print(Got: , result)
 
def after(delay, gen):
    yield pool.submit(time.sleep, delay)
    result = None
    try:
        while True:
            f = gen.send(result)
            result = yield f
    except StopIteration:
        print(after ecoutnered StopIteration.)
 
class Task:
    def __init__(self, gen):
        self._gen = gen
    def step(self, value=None):
        # Run to the next yield
        try:
            fut = self._gen.send(value)
            # Future returned
            fut.add_done_callback(self._wakeup)
        except StopIteration as exc:
            print(Task encountered StopIteration.)
    def _wakeup(self, fut):
        # handler for results
        result = fut.result()
        self.step(result)  # Feedback loop (run to next yield)

为查看完整图片,可右键单击图片选择在新页面中查看。

技术分享图片

 

yield学习笔记

标签:val   ini   worker   image   return   inf   img   hand   after   

原文地址:https://www.cnblogs.com/yifeixu/p/8734899.html

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