M
M
Michael Kim2017-02-23 17:47:39
Python
Michael Kim, 2017-02-23 17:47:39

How to convert a synchronous function into an asynchronous one?

Imagine there is a function

def calc():
        i = 1
        while i < 100000000:
            i += 1
        return 'result'

It is synchronous and will block the thread.
You can execute it in another thread:
from concurrent.futures import ThreadPoolExecutor, run_on_executor

executor = ThreadPoolExecutor(max_worker=4)

@run_on_executor
def calc():
        i = 1
        while i < 100000000:
            i += 1
        return 'result'

But is it really possible to turn a synchronous function in Tornado into an asynchronous one in this way?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question