gpt4 book ai didi

python - 多线程XML-RPC (python3.7.1)

转载 作者:行者123 更新时间:2023-12-02 03:25:11 25 4
gpt4 key购买 nike

服务器:

  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 的讲座吗?

最佳答案

您发现的问题是只能在特定端口上监听一次。这意味着您不需要启动多个线程来监听,而是需要一个将响应委托(delegate)给不同线程的监听器。

Python 提供了现成的 multithreaded server它可以与内置的服务器类结合使用,例如 SimpleXMLRPCServer

下面的代码实现了一个多线程 sleep 服务器,下面是多线程客户端和输出,注意服务器和客户端之间 sleep 时间的不同顺序。

多线程服务器:

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

关于python - 多线程XML-RPC (python3.7.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53621682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com