如何在线程中运行aiohttp服务器?

时间:2018-07-31 09:30:56

标签: python python-asyncio aiohttp

此aiohttp服务器在线程中的示例失败,并显示RuntimeError: There is no current event loop in thread 'Thread-1'.错误:

import threading
from aiohttp import web


def aiohttp_server():
    def say_hello(request):
        return web.Response(text='Hello, world')

    app = web.Application(debug=True)
    app.add_routes([web.get('/', say_hello)])
    web.run_app(app)


t = threading.Thread(target=aiohttp_server)
t.start()

如何在线程中运行aiohttp服务器?

4 个答案:

答案 0 :(得分:1)

我们必须在主线程中使用app.make_handler处理程序,例如:

import asyncio
import threading
from aiohttp import web

loop = asyncio.get_event_loop()


def say_hello(request):
    return web.Response(text='Hello, world')


app = web.Application(debug=True)
app.add_routes([web.get('/', say_hello)])

handler = app.make_handler()
server = loop.create_server(handler, host='127.0.0.1', port=8080)


def aiohttp_server():
    loop.run_until_complete(server)
    loop.run_forever()


t = threading.Thread(target=aiohttp_server)
t.start()

答案 1 :(得分:1)

在主线程中创建handler,然后在子线程中手动创建事件循环。

import asyncio
import threading
from aiohttp import web


def aiohttp_server():
    def say_hello(request):
        return web.Response(text='Hello, world')

    app = web.Application(debug=True)
    app.add_routes([web.get('/', say_hello)])
    handler = app.make_handler()
    return handler


def run_server(handler):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    server = loop.create_server(handler, host='127.0.0.1', port=8089)
    loop.run_until_complete(server)
    loop.run_forever()


t = threading.Thread(target=run_server, args=(aiohttp_server(),))
t.start()

答案 2 :(得分:0)

针对此用例有一个新的API:

https://docs.aiohttp.org/en/stable/web_advanced.html#application-runners

from aiohttp import web
import asyncio


async def healthz(request):
    return web.Response(text="OK")

app = web.Application()
app.add_routes([web.get("/", healthz)])


async def runner():
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, "localhost", 8080)
    await site.start()


loop = asyncio.get_event_loop()
loop.run_until_complete(runner())

答案 3 :(得分:0)

可以使用web.run_app,只需创建一个新的事件循环(并设置handle_signals=False来避免RuntimeError: set_wakeup_fd only works in main thread):

import threading
import asyncio
from aiohttp import web

def aiohttp_server():
    def say_hello(request):
        return web.Response(text='Hello, world')

    asyncio.set_event_loop(asyncio.new_event_loop())  # create a new event loop here
    app = web.Application(debug=True)
    app.add_routes([web.get('/', say_hello)])
    web.run_app(app, handle_signals=False)