2

I'm trying to use the tornado event loop to do a series of tasks in parallel. So I'm creating an event loop and creating a method, decorated by @gen.coroutine which yields a list of gen.Task calls to another method.

from tornado import ioloop, gen

class MyClass(object):                                           
@gen.coroutine                                               
def echo(text):                                              
    print text                                               
    return gen.Return()                                      

@gen.coroutine                                               
def send_messages(self, number):                             
    yield [gen.Task(self.echo, x) for x in xrange(number)]   
    self.loop.stop()                                         

def __init__(self):                                          
    self.loop = ioloop.IOLoop()                              
    self.loop.add_callback(self.send_messages, 3)            
    self.loop.start()                                        

MyClass()                                                        

It seems okay to me but doesn't work. When I run this I get a "TypeError: 'NoneType' object is not iterable" error in tornado/concurrent.py.

Any idea what I'm doing wrong here?


To help debug, I found that putting a pdb somewhere in the send_messages() method and doing self._echo("test").result() helps a lot.

4

2 に答える 2

1

関数の署名が間違っているため、functools.partialバインディングを台無しにしています:

@gen.coroutine                                               
def echo(text):      # <----- MISSING self                                 
    print text                                               
    return gen.Return()

を追加selfすれば準備完了です。

于 2013-07-17T22:55:40.077 に答える