簡體   English   中英

如何從python程序運行許多aiohttp服務器

[英]How run many aiohttp servers from python program

我如何從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()

一切都會好的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM