如何从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)
答案 0 :(得分:0)
我找到了答案
<强>第一强>
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()
一切都会好的!