多线程 XML-RPC (python3.7.1)
Multi-threaded XML-RPC (python3.7.1)
服务器:
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()
客户:
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())
问题:
我无法创建多个 ServerThread 实例都侦听同一个端口,抛出异常。
我希望看到所有 4 个线程并行执行。
我错过了什么?后面有关于 GIL 的讲座吗?
您发现的问题是只能在特定端口上收听一次。这意味着您不需要启动多个线程来侦听,而是需要一个将响应委托给不同线程的侦听器。
Python 提供现成的 multithreaded server 可以与内置服务器 类 结合使用,例如 SimpleXMLRPCServer
.
以下代码实现了多线程睡眠服务器,多线程客户端和输出见下文,注意服务器和客户端之间睡眠时间的不同顺序。
多线程服务器:
import random
import time
from socketserver import ThreadingMixIn
from xmlrpc.server import SimpleXMLRPCServer
class SimpleThreadedXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):
pass
# 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)
server = SimpleThreadedXMLRPCServer(server_addr)
server.register_function(sleep, 'sleep')
print("Server thread started. Testing server ...")
print('listening on {} port {}'.format(host, port))
server.serve_forever()
if __name__ == '__main__':
run_server()
多线程客户端:
import xmlrpc.client
from concurrent.futures import ThreadPoolExecutor, as_completed
def submit_sleep():
server = xmlrpc.client.ServerProxy("http://localhost:8000/", allow_none=True)
return server.sleep()
with ThreadPoolExecutor() as executor:
sleeps = {executor.submit(submit_sleep) for _ in range(4)}
for future in as_completed(sleeps):
sleep_time = future.result()
print(sleep_time)
服务器输出:
Server thread started. Testing server ...
listening on localhost port 8000
sleeping 3 seconds
sleeping 9 seconds
sleeping 3 seconds
sleeping 10 seconds
127.0.0.1 - - [05/Dec/2018 14:32:02] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [05/Dec/2018 14:32:02] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [05/Dec/2018 14:32:08] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [05/Dec/2018 14:32:09] "POST / HTTP/1.1" 200 -
客户端输出:
slept 3 seconds, exiting
slept 3 seconds, exiting
slept 9 seconds, exiting
slept 10 seconds, exiting
服务器:
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()
客户:
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())
问题:
我无法创建多个 ServerThread 实例都侦听同一个端口,抛出异常。
我希望看到所有 4 个线程并行执行。
我错过了什么?后面有关于 GIL 的讲座吗?
您发现的问题是只能在特定端口上收听一次。这意味着您不需要启动多个线程来侦听,而是需要一个将响应委托给不同线程的侦听器。
Python 提供现成的 multithreaded server 可以与内置服务器 类 结合使用,例如 SimpleXMLRPCServer
.
以下代码实现了多线程睡眠服务器,多线程客户端和输出见下文,注意服务器和客户端之间睡眠时间的不同顺序。
多线程服务器:
import random
import time
from socketserver import ThreadingMixIn
from xmlrpc.server import SimpleXMLRPCServer
class SimpleThreadedXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):
pass
# 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)
server = SimpleThreadedXMLRPCServer(server_addr)
server.register_function(sleep, 'sleep')
print("Server thread started. Testing server ...")
print('listening on {} port {}'.format(host, port))
server.serve_forever()
if __name__ == '__main__':
run_server()
多线程客户端:
import xmlrpc.client
from concurrent.futures import ThreadPoolExecutor, as_completed
def submit_sleep():
server = xmlrpc.client.ServerProxy("http://localhost:8000/", allow_none=True)
return server.sleep()
with ThreadPoolExecutor() as executor:
sleeps = {executor.submit(submit_sleep) for _ in range(4)}
for future in as_completed(sleeps):
sleep_time = future.result()
print(sleep_time)
服务器输出:
Server thread started. Testing server ...
listening on localhost port 8000
sleeping 3 seconds
sleeping 9 seconds
sleeping 3 seconds
sleeping 10 seconds
127.0.0.1 - - [05/Dec/2018 14:32:02] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [05/Dec/2018 14:32:02] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [05/Dec/2018 14:32:08] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [05/Dec/2018 14:32:09] "POST / HTTP/1.1" 200 -
客户端输出:
slept 3 seconds, exiting
slept 3 seconds, exiting
slept 9 seconds, exiting
slept 10 seconds, exiting