Discussion:
[tornado] Async task get exception: has no attribute '_stack_context_handle_exception'
l***@gmail.com
2015-06-24 12:23:05 UTC
Permalink
Hi all,

I'd like to write an async task which is cpu-bound and time-consuming, so i
use ThreadPool and asynchronous.
But i got AttributeError: Test instance has no attribute
'_stack_context_handle_exception'

The code like below:

import tornado.ioloop
import tornado.web
import tornado.gen
import asynctorndb
import time
import tornado.process
import subprocess
from tornado.gen import Task, Return, coroutine
from multiprocessing.pool import ThreadPool
from tornado.ioloop import IOLoop
from tornado.web import Application, asynchronous, RequestHandler
from time import sleep
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor

_workers = ThreadPool(10)


class Test:
def run_background(self, func, callback, args=(), kwds={}):
def _callback(result):
IOLoop.instance().add_callback(lambda: callback(result))
_workers.apply_async(func, args, kwds, _callback)


def blocking_task(self, n):
print n
sleep(n)
print 'sleep ok'
return n


@asynchronous
def myfunc(self):
self.run_background(self.blocking_task, self.on_complete, (5,))


def on_complete(self, res):
n = res
print n
self.write("Hello, world")
self.finish()




class MainHandler1(tornado.web.RequestHandler):
def get(self):
test = Test()
test.myfunc()
self.write("Hello, world")
self.finish()


class MainHandler2(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
self.finish()


application = tornado.web.Application([
(r"/a", MainHandler1),
(r"/b", MainHandler2),
])


if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
AttributeError: Test instance has no attribute
'_stack_context_handle_exception'
I am newb to tornado.Can anyone help me with this?
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ben Darnell
2015-06-25 03:19:24 UTC
Permalink
The tornado.web.asynchronous decorator should only be applied to the http
methods of RequestHandler (get(), post(), etc); it is undefined in all
other cases.

The @gen.coroutine decorator offers a much more convenient way to work with
ThreadPoolExecutor: you can simply yield the Futures returned by
Executor.submit().

-Ben
Post by l***@gmail.com
Hi all,
I'd like to write an async task which is cpu-bound and time-consuming, so
i use ThreadPool and asynchronous.
But i got AttributeError: Test instance has no attribute
'_stack_context_handle_exception'
import tornado.ioloop
import tornado.web
import tornado.gen
import asynctorndb
import time
import tornado.process
import subprocess
from tornado.gen import Task, Return, coroutine
from multiprocessing.pool import ThreadPool
from tornado.ioloop import IOLoop
from tornado.web import Application, asynchronous, RequestHandler
from time import sleep
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor
_workers = ThreadPool(10)
IOLoop.instance().add_callback(lambda: callback(result))
_workers.apply_async(func, args, kwds, _callback)
print n
sleep(n)
print 'sleep ok'
return n
@asynchronous
self.run_background(self.blocking_task, self.on_complete, (5,))
n = res
print n
self.write("Hello, world")
self.finish()
test = Test()
test.myfunc()
self.write("Hello, world")
self.finish()
self.write("Hello, world")
self.finish()
application = tornado.web.Application([
(r"/a", MainHandler1),
(r"/b", MainHandler2),
])
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Post by l***@gmail.com
AttributeError: Test instance has no attribute
'_stack_context_handle_exception'
I am newb to tornado.Can anyone help me with this?
--
You received this message because you are subscribed to the Google Groups
"Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...