Server:
import time
import random
from threading import Thread
from xmlrpc.server import SimpleXMLRPCServer
class ServerThread(Thread):
def __init__(self, server_addr):
┆ Thread.__init__(self)
┆ self.server = SimpleXMLRPCServer(server_addr)
┆ self.server.register_function(sleep, 'sleep')
def run(self):
┆ self.server.serve_forever()
# sleep for random number of seconds
def sleep():
r = random.randint(2,10)
print('sleeping {} seconds'.format(r))
time.sleep(r)
return 'slept {} seconds, exiting'.format(r)
# run server
def run_server(host="localhost", port=8000):
server_addr = (host, port)
thread1 = ServerThread(server_addr)
thread1.start()
print("Server thread started. Testing server ...")
print('listening on {} port {}'.format(host, port))
if __name__ == '__main__':
run_server()
Client:
import xmlrpc.client
server = xmlrpc.client.ServerProxy("http://localhost:8000/", allow_none=True)
print(server.sleep())
print(server.sleep())
print(server.sleep())
print(server.sleep())
Question:
I can't create multiple ServerThread instances all listening to the same port, throws exception.
I would like to see all 4 threads executing in parallel.
What am I missing? Does a lecture on GIL follow?