Threadloop: Tornado IOLoop Backed Concurrent Futures

1.0.2 · maintenance · verified Thu Apr 16

Threadloop is a Python library that enables running Tornado coroutines from synchronous Python code by leveraging a thread pool, backed by Tornado's IOLoop. It provides a `ThreadLoopExecutor` that mimics the `concurrent.futures.ThreadPoolExecutor` API. The library is currently at version 1.0.2 and was last updated in 2016, indicating it is no longer actively maintained but generally functional.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `ThreadLoopExecutor` as a context manager and submit both Tornado coroutines and regular synchronous functions. The `submit` method returns a future, and `.result()` can be called to block until the task completes and retrieve its return value. Note that `tornado.gen.coroutine` is used for demonstration, which is an older decorator; modern Tornado often uses `async def` functions.

import tornado.gen
from threadloop import ThreadLoopExecutor

@tornado.gen.coroutine
def my_coroutine(name):
    print(f"Hello from {name} in coroutine!")
    yield tornado.gen.sleep(0.1) # Simulate async work
    print(f"Goodbye from {name}!")
    return f"Result from {name}"

def sync_function(x, y):
    print(f"Running sync_function with {x} and {y}")
    return x + y

if __name__ == '__main__':
    # Example 1: Running a Tornado coroutine in the ThreadLoopExecutor
    with ThreadLoopExecutor() as executor:
        future_coro = executor.submit(my_coroutine, "CoroutineWorker")
        result_coro = future_coro.result() # Blocks until coroutine completes
        print(f"Coroutine result: {result_coro}")

    # Example 2: Running a regular synchronous function
    with ThreadLoopExecutor() as executor:
        future_sync = executor.submit(sync_function, 10, 20)
        result_sync = future_sync.result()
        print(f"Sync function result: {result_sync}")

view raw JSON →