Below is the code of eventlet server. I want to do Non-blocking IO. To test the non-blocking IO, i used below code as server.
import eventlet
from eventlet.green import socket
def fib(n):
if n == 1 or n == 2:
return 1
return (fib(n-1) + fib(n-2))
def handle_socket(reader, writer):
print ("client connected")
while True:
line = reader.readline()
if not line:
break
writer.write(line)
writer.flush()
n = line.rstrip()
print ("echoed", int(n))
print(fib(int(n)))
print ("disconnected")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 6000))
server.listen(100)
print ("server listening on port 6000")
print('called me...')
while True:
sock, addr = server.accept()
eventlet.spawn(handle_socket, sock.makefile('r'), sock.makefile('w'))
To test at the client side,(on windows), did below steps,
telnet localhost 6000
35
To get the 35th Fibonacci series number, on my laptop it takes around 15secs. Meanwhile, i open another terminal and give input of smaller fibonacci number like 5 or 6(which takes like 2/3 secs). But this server code is running sequentially, after the output of the 35th number is calculated then only the other smaller number output is printed. Is there is way to make the same code "concurrent" or "parallelism".
Your best option is to run blocking code in separate OS thread. Eventlet has built in thread pool in
eventlet.tpool
[1]. Simpler API for single function call:[1] http://eventlet.net/doc/threading.html#tpool-simple-thread-pool