Comments (7)
@oneforgit 那就多进程开多个 NoneBot(
from nonebot.
多进程? 多线程?
查了一下,tornado 框架也有类似的问题,但是tornado有解决的办法 : asyncio.AnyThreadEventLoopPolicy
Quart 貌似网上也没人说如何解决 Quart 如何设置指定的线程(thrading.Thread)
from nonebot.
好像可以通过设置新的loop来处理线程提示那个错误,
asyncio.set_event_loop(asyncio.new_event_loop())
loop = asyncio.get_event_loop()
from nonebot.
好像可以通过设置新的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.
@oneforgit Quart run 的时候可以传入 loop 参数
from nonebot.
@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.
@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)
- 请问如何在只是User is calling me的情况下保持会话? HOT 2
- scheduler与message_preprocessor不在同一个eventloop导致两者之间很难进行同步 HOT 6
- nonebot+cqhttp反向websocket 时出现错误: websocket: bad handshake HOT 8
- [提问]如何关闭heartbeat的logging HOT 6
- aget 无法发送prompt HOT 3
- 1.8.3移除`CommandSession.args`导致类 Shell 参数解析Command出错 HOT 2
- 以 node-onebot 为后端无法正确设置 `ctx['to_me']` HOT 5
- 加載“失敗”的插件注冊的命令仍然可用 HOT 1
- 考慮移除 sched HOT 1
- NLPSession无法处理被两个qq表情夹着的关键词 HOT 11
- 运行`nb run`命令后直接抛出`cannot import name 'WebSocketSetup' from 'nonebot.drivers' ` HOT 1
- say 和 echo失踪 HOT 3
- NLPSession在较长的句子中不能识别到关键词 HOT 2
- 1
- 关于Pyinstaller打包后的exe为什么会报错的解决方案【建议调整一部分代码】
- 启动时遇到ImportError HOT 2
- m1 运行报错cannot import name 'escape' from 'jinja2' HOT 9
- ImportError: cannot import name 'overrides' from 'nonebot.typing' HOT 1
- 无法正确识别“回复”类型消息中的指令 HOT 2
- 按组一同注册命令的关键词 内容 权限 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nonebot.