I try to understand concurrency from David Beazley talks. But when executing the server and client and try to submit the number 20 fro client, is seem that the futur object block forever when calling futur.result(). I can't understand why:
# server.py
# Fib microservice
from socket import *
from fib import fib
from threading import Thread
from concurrent.futures import ProcessPoolExecutor as Pool
pool = Pool(4)
def fib_server(address):
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(5)
while True:
client, addr = sock.accept()
print("Connection", addr)
Thread(target=fib_handler, args=(client,), daemon=True).start()
def fib_handler(client):
while True:
req = client.recv(100)
if not req:
break
n = int(req)
future = pool.submit(fib, n)
#Next line will block!!!!
result = future.result()
resp = str(result).encode('ascii') + b'\n'
client.send(resp)
print("Closed")
fib_server(('',25000))
#client.py
import socket
s = socket.socket()
s.connect(('localhost',25000))
while True:
num=input("number?")
s.send(str(num).encode('ascii') + b'\n')
res = s.recv(1000)
print('res:',res)
server> python server.py
client> python client.py
We see in order:
server> Connection ('127.0.0.1', 57876)
client> number?20
server>[freeze]