Giter VIP home page Giter VIP logo

Comments (7)

stdrc avatar stdrc commented on July 21, 2024

@oneforgit 那就多进程开多个 NoneBot(

from nonebot.

oneforgit avatar oneforgit commented on July 21, 2024

多进程? 多线程?

查了一下,tornado 框架也有类似的问题,但是tornado有解决的办法 : asyncio.AnyThreadEventLoopPolicy
Quart 貌似网上也没人说如何解决 Quart 如何设置指定的线程(thrading.Thread)

from nonebot.

puti-flower avatar puti-flower commented on July 21, 2024

好像可以通过设置新的loop来处理线程提示那个错误,

asyncio.set_event_loop(asyncio.new_event_loop())
loop = asyncio.get_event_loop()

from nonebot.

oneforgit avatar oneforgit commented on July 21, 2024

好像可以通过设置新的loop来处理线程提示那个错误,

asyncio.set_event_loop(asyncio.new_event_loop())
loop = asyncio.get_event_loop()

是的,asyncio 可以通过set_event_loop 指定对应的线程,但是通过Quart封装之后,不知道如何在Quart中指定,或者Quart中有没有对应的方法解决,还需要翻阅Quart user guide.

from nonebot.

stdrc avatar stdrc commented on July 21, 2024

@oneforgit Quart run 的时候可以传入 loop 参数

from nonebot.

oneforgit avatar oneforgit commented on July 21, 2024

@oneforgit Quart run 的时候可以传入 loop 参数

还是搞不定,大佬能帮我改一下吗?

from quart import Quart

import threading
import asyncio

class botThread(threading.Thread):
    def __init__(self, parent=None):
        super().__init__(parent)
        
    def run(self):
        """
        case#1: if we use get_event_loop, by default, it creates loop at main thread
        thus it will cause error:
            RuntimeError: There is no current event loop in thread 'Thread-1'.
            
        loop = asyncio.get_event_loop()
        """      
        
        """
        case#2: if we leverage new_event_loop() and set_event_loop(loop). 
        it will be working for normal asyncio, but it doesn't work for quart frame.
        error:
            raise RuntimeError('This event loop is already running')
        """
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        thread_loop = asyncio.get_event_loop()
        loop.run_until_complete(work(thread_loop))

async def work(loop = None):
    app = Quart(__name__)
         
    @app.route('/')
    async def hello():
        return 'hello'
     
    @app.route('/login')
    async def login():
        return 'login page!'
    
    await app.run(loop = loop)
        
if __name__ == '__main__':
    botThread().start()

from nonebot.

stdrc avatar stdrc commented on July 21, 2024

@oneforgit 试了下,Quart 似乎确实必须在主线程运行。

有两种办法,一种是,在同一个 loop 上运行多个 Quart,另一种是用多进程运行多个 Quart:

单 loop 多实例

import asyncio

import hypercorn
from hypercorn.asyncio import serve
from quart import Quart


async def serve_quart(port: int):
    app = Quart(f'{__name__}_{port}')

    @app.route('/')
    async def index():
        return f'hello from port {port}'

    config = hypercorn.Config()
    config.port = port
    await serve(app, config)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(
        asyncio.gather(serve_quart(8080), serve_quart(8081))
    )

多进程多实例

from multiprocessing import Process

from quart import Quart


def run_quart(port: int):
    app = Quart(f'{__name__}_{port}')

    @app.route('/')
    async def index():
        return f'hello from port {port}'

    app.run(port=port)


if __name__ == '__main__':
    p = Process(target=run_quart, args=(8080,))
    p.start()

    run_quart(8081)

from nonebot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.