Suppose I have a long running function:
def long_running_function():
result_future = Future()
result = 0
for i in xrange(500000):
result
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:
I'm doing the similar implementation now, and will update this answer when I have more progress :)
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.