[英]return multiple generators python
這是關於flask子函數未產生結果的參考
我如何返回多個發電機,像這樣...
當前,無論我嘗試什么,它都只會執行第一個,例如,如果我列出了一系列生成器並循環遍歷,那么它仍然會執行第一個。
有什么想法嗎?
#!/usr/bin/env python
import flask
import time
class TestClass(object):
def __init__(self):
pass
def worker(self):
a='1234'
b=a + '45\n'
yield b
time.sleep(3)
yield a
def worker2(self):
time.sleep(3)
c = '9876'
yield c
tc = TestClass()
app = flask.Flask(__name__)
@app.route('/')
def test_method_get_stuff():
return flask.render_template('index.html')
@app.route('/', methods=['POST'])
def test_method_post_stuff():
def test_method_sub_function():
return tc.worker()
return tc.worker2()
return flask.Response(test_method_sub_function(),mimetype= 'text/plain')
app.run(debug=True)
當使用return
您退出該函數 ; 該行之后的任何內容都不會執行。
相反,您必須鏈接生成器。 在此處使用itertools.chain()
:
from itertools import chain
@app.route('/', methods=['POST'])
def test_method_post_stuff():
def test_method_sub_function():
return chain(tc.worker(), tc.worker2())
return flask.Response(test_method_sub_function(),mimetype= 'text/plain')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.