2017-08-29 61 views
1

如何從1個Python程序運行許多aiohttp服務器 例子:如何運行從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

https://stackoverflow.com/questions/44850701/multiple-aiohttp-applications-running-in-the-same-process – Juggernaut

回答

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() 

而且一切都會好的!

相關問題