Anton
Anton

Reputation: 171

Tornado framework. TypeError: 'Future' object is not callable

I've started to learn Tornado framework sometime ago. I've faced the lack of documentation for unexperienced users and checked also asyncio module docs. So the problem is, that I have some simple code in asyncio:

    import asyncio

    @asyncio.coroutine
    def compute(x, y):
        print("Compute %s + %s ..." % (x, y))
        yield from asyncio.sleep(1.0)
        return x + y


    @asyncio.coroutine
    def print_sum(x, y):
        result = yield from compute(x, y)
        print("%s + %s = %s" % (x, y, result))


loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()

And then I've tried to make the same using Tornado framework:

from tornado.ioloop import IOLoop
from tornado import gen

@gen.coroutine
def compute(x, y):
   print("Compute %s + %s ..." % (x, y))
   yield gen.sleep(1.0)
   return (x+y)

@gen.coroutine
def print_sum(x, y):
   result = yield compute(x, y)
   print("%s + %s = %s" % (x, y, result))

IOLoop.instance().run_sync(print_sum(1,2))

But unfortunately Tornado code raise such an exception:

 Compute 1 + 2 ...
Traceback (most recent call last):
  File "tornado_coroutine.py", line 19, in <module>
    IOLoop.instance().run_sync(print_sum(1, 2))
  File "C:\Python34\lib\site-packages\tornado\ioloop.py", line 421, in run_sync
    return future_cell[0].result()
  File "C:\Python34\lib\site-packages\tornado\concurrent.py", line 209, in resul
t
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "C:\Python34\lib\site-packages\tornado\ioloop.py", line 402, in run
    result = func()
TypeError: 'Future' object is not callable

Maybe IOLoop tries to make an new "lap" after all coroutines have returned their values?

Upvotes: 11

Views: 6277

Answers (1)

Klaus D.
Klaus D.

Reputation: 14369

run_sync takes a function (or other "callable") as argument. You are calling the function in-place and then giving the result as argument. You can create an anonymous function simply by using lambda:

IOLoop.instance().run_sync(lambda: print_sum(1,2))

Upvotes: 20

Related Questions