Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python语法错误:(“带参数的”return&“x27”;在生成器“中,”)_Python_Return_Generator_Tornado - Fatal编程技术网

Python语法错误:(“带参数的”return&“x27”;在生成器“中,”)

Python语法错误:(“带参数的”return&“x27”;在生成器“中,”),python,return,generator,tornado,Python,Return,Generator,Tornado,我的Python程序中有以下函数: @tornado.gen.engine def check_status_changes(netid, sensid): como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), '&sensid=', str(sensid), '&start=-5s&end=-1s']) http_client = AsyncHTTPC

我的Python程序中有以下函数:

@tornado.gen.engine
def check_status_changes(netid, sensid):        
    como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), '&sensid=', str(sensid), '&start=-5s&end=-1s'])

    http_client = AsyncHTTPClient()
    response = yield tornado.gen.Task(http_client.fetch, como_url)

    if response.error:
            self.error("Error while retrieving the status")
            self.finish()
            return error

    for line in response.body.split("\n"):
                if line != "": 
                    #net = int(line.split(" ")[1])
                    #sens = int(line.split(" ")[2])
                    #stype = int(line.split(" ")[3])
                    value = int(line.split(" ")[4])
                    print value
                    return value
我知道

for line in response.body.split

这是一台发电机。但我会将value变量返回给调用该函数的处理程序。这有可能吗?我该怎么做?

在Python 2或Python 3.0-3.2中,不能使用带有值的
return
退出生成器。您需要使用
yield
加上
return
而不使用表达式:

if response.error:
    self.error("Error while retrieving the status")
    self.finish()
    yield error
    return
在循环本身中,再次使用
yield

for line in response.body.split("\n"):
    if line != "": 
        #net = int(line.split(" ")[1])
        #sens = int(line.split(" ")[2])
        #stype = int(line.split(" ")[3])
        value = int(line.split(" ")[4])
        print value
        yield value
        return
替代方法是引发异常或使用tornado回调


在Python3.3及更新版本中,
return
在生成器函数中带有一个值,结果该值被附加到
StopIterator
异常。对于
async def
异步生成器(Python3.6及更高版本),
return
的值必须仍然较小

已经试过了。。但我得到了同样的错误。。。我认为不可能将回报放入生成器中……for的
循环不是生成器;整个函数是因为其中有一个
yield
语句。感谢您的解释!