2013-04-04 43 views
16

我有我的Python程序这个功能:的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 

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

+0

'yield value'。 – katrielalex 2013-04-04 11:02:12

+0

已经尝试过..但我得到了同样的错误......我认为这是不可能把一个返回到发电机... – sharkbait 2013-04-04 11:03:54

+9

的'for'循环不是一台发电机;整个功能是因为你有一个'yield'语句。 – geoffspear 2013-04-04 11:05:37

回答

23

不能使用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 

替代品是引发异常或改用龙卷风回调。

+0

太棒了!我认为这个错误是在'返回值'中 – sharkbait 2013-04-04 11:23:08