Python Tornado - Confused how to convert a blocking function into a non-blocking function

前端 未结 2 638
猫巷女王i
猫巷女王i 2021-01-04 21:41

Suppose I have a long running function:

def long_running_function():
    result_future = Future()
    result = 0
    for i in xrange(500000):
        result          


        
相关标签:
2条回答
  • 2021-01-04 22:18

    I'm currently struggling to add a web interface for my simulation program using Tornado and its WebSocket function. My simulation program is computationally intensive, i.e., CPU-bound task as said by @ben-darnell , which should be implemented using another thread or process.

    After many investigations, I think these resources may be helpful:

    • Tornado blocking asynchronous requests - Answer by @koblas
    • Trying to call a long blocking function in tornado in a non blocking way

    I'm doing the similar implementation now, and will update this answer when I have more progress :)

    0 讨论(0)
  • 2021-01-04 22:19

    If the blocking function is CPU-bound (as your for/xrange example is), then threads (or processes) are the only way to make it non-blocking. Creating a thread per incoming request is expensive, but making a small ThreadPoolExecutor to handle all CPU-bound operations is not.

    To make a function non-blocking without using threads, the function must be event-driven: it must be waiting on some external event (such as network I/O) so that it can be awoken when that event occurs.

    0 讨论(0)
提交回复
热议问题