-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample3.py
More file actions
32 lines (27 loc) · 737 Bytes
/
example3.py
File metadata and controls
32 lines (27 loc) · 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# example2.py
# Python concurrency
from socket import *
from threading import Thread
from concurrent.futures import ProcessPoolExecutor as Pool
from fib import fib
pool = Pool(4)
def high_cpu_usage_function(client):
while True:
req = client.recv(100)
if not req:
break
fib_num = int(req)
future = pool.submit(fib, fib_num)
result = future.result()
resp = str(result).encode('ascii') + b'\n'
client.send(resp)
def 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 accepted", addr)
Thread(target=high_cpu_usage_function, args=(client, )).start()
server(("", 9000))