Migrating Twisted code to Tornado code " threads.deferToThread"

131 views
Skip to first unread message

Hector Fernandez

unread,
Mar 12, 2014, 3:45:10 PM3/12/14
to python-...@googlegroups.com
Hi everybody,

I am trying to migrate Twisted code of my app to Tornado equivalents. In particular, I am experiencing some problems when trying to find equivalents to the following Twisted code:

threads.deferToThread(try_func_call, func_call, *args, **kwargs)


def return_value(value):
    defer.returnValue(value)

def function(func_call, *args, **kwards):
    try:
        result = yield threads.deferToThread(try_func_call, func_call, *args, **kwargs)
    except:
        print traceback.format_exc()
    if isinstance(result, Exception):
        raise result

    return_value(result)

Could you give a solution ? I don't want to keep Twisted code in my Tornado app.

Thanks in advance.

Best,
Hector

A. Jesse Jiryu Davis

unread,
Mar 12, 2014, 4:35:28 PM3/12/14
to python-...@googlegroups.com
You'll want a ThreadPoolExecutor from the "concurrent.futures" package, which is included in Python 3's standard library. In Python 2, install the "futures" package.

# Do this at global scope.
executor = ThreadPoolExecutor(4)  # number of threads

@gen.coroutine
def caller():
    # Yields to IOLoop. Resumes after callee completes on worker thread.
    yield executor.submit(callee)




--
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-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages