我必须创建一个函数,在内部调用中做一些艰苦的工作。此函数必须是生成器,因为我使用的是服务器发送的事件。因此,我希望这个函数通过使用“yield”来通知计算的进度。之后,此函数必须将结果传递给父函数,以便继续进行其他计算。你知道吗
我想要这样的东西:
def hardWork():
for i in range(N):
# hard work
yield 'Work done: ' + str(i)
# Here is the problem: I can't return a result if I use a yield
return result
def generator():
# do some calculations
result = hardWork()
# do other calculations with this result
yield finalResult
我发现了一个解决方案,它包含一个字典,告诉我们函数是否已经完成,但是执行这个操作的代码非常脏。你知道吗
还有别的解决办法吗?你知道吗
谢谢你!你知道吗
我的想法是:
def innerFunction(gen):
calc = 1
for iteration in range(10):
for i in range(50000):
calc *= random.randint(0, 10)
gen.send(iteration)
yield calc
def calcFunction(gen):
gen2 = innerFunction(gen)
r = next(gen2)
gen.send("END: " + str(r + 1))
gen.send(None)
def notifier():
while True:
x = yield
if x is None:
return
yield "Iteration " + x
def generator():
noti = notifier()
calcFunction(noti)
yield from noti
for g in generator():
print(g)
但我收到一个错误:
TypeError: can't send non-None value to a just-started generator
我的建议是将您的功能嵌入到类中。你知道吗
这将具有您想要的功能,而不必担心围绕异常抛出捕获逻辑编程逻辑。你知道吗
Python3.5之前:生成器
这个解决方案也适用于较新的Python版本,尽管Python3.5中的新版本
async def
似乎更适合您的用例。请参见下一节。你知道吗生成器生成的值是通过迭代或使用
next
获得的。最后返回的值存储在StopIteration
异常的value
属性中,该异常指示生成器的结束。幸运的是,恢复并不太难。你知道吗示例
输出
Python3.5+:
async def
如果您运行的是Python3.5+,那么您所尝试的似乎非常适合使用等待函数的事件循环。你知道吗
输出
python3.3引入了^{} for generator delegation ,这正是您所要求的。
yield from
允许主生成器将功能委托给另一个也包含yield
语句的函数,如下所示:相关问题 更多 >
编程相关推荐