Python Tornado - Confused How To Convert A Blocking Function Into A Non-blocking Function
Solution 1:
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.
Solution 2:
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 :)
Post a Comment for "Python Tornado - Confused How To Convert A Blocking Function Into A Non-blocking Function"