gpt4 book ai didi

python - 如何从python程序运行许多aiohttp服务器

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

我如何从1个python程序运行许多aiohttp服务器
例子:

 manager = Manager()
server1 = manager.create_server(config1)
server2 = manager.create_server(config2)
server1.run() # here program stop
server2.run() # but i want to run these two servers at the same time

我正在尝试使用threading.Thread()创建多个线程并在其中运行服务器
但是得到这个错误:
RuntimeError: There is no current event loop in thread 'thname'

我尝试使用loop.run_in_executor(),但这种方式什么也没有发生,
程序完成而没有错误,并且服务器未运行。

这是服务器 运行函数
    def run(self, port, host):
app = web.Application()
app.router.add_post('/', self._get_update)
web.run_app(app, host=host, port=port)

最佳答案

我找到答案

第一

def run(self, host, port):
app = web.Application() # make app
app.router.add_post('/', self._get_update) # add handlers
handler = app.make_handler() # make handlers
server = loop.create_server(handler, host, port) #create server
loop.run_until_complete(server) # this is the most important string, as i'v understood it run server on the loop and then return loop to a waiting state.
print("======== Running on {} ========\n".format(host+":"+str(port))) # info message

所以接下来我们可以做
server1.run(host, port1)
server2.run(host, port2)
loop.run_forever()

一切都会好的!

关于python - 如何从python程序运行许多aiohttp服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943663/

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