python-3.x - 在后台运行异步数据库读取任务
问题描述
我有一个函数get_active_allowed_systems_by_poll
,我想在后台调用它 10 次/分钟,并刷新我在过去 10 秒内获得的新系统。
import asyncio
from threading import Thread
async def create_state_machine_at_init(app):
worker_loop = asyncio.new_event_loop()
worker = Thread(target=start_db_worker, args=(worker_loop,))
worker.start()
worker_loop.call_soon_threadsafe(get_active_allowed_systems_by_poll, app, 30)
async def get_active_allowed_systems_by_poll(app, interval=10):
params = {
param: key
for param, key
in app.config.get_active_allowed_systems_by_poll_params.items()
}
params['interval'] = interval
operation = prepare_exec(
app.config.get_active_allowed_systems_by_poll,
**params
)
ACTIVE_ALLOWED_SYSTEMS
ACTIVE_ALLOWED_SYSTEMS = (await app['database'].execute(operation)).all()
return ACTIVE_ALLOWED_SYSTEMS
def start_db_worker(loop):
"""Switch to new event loop and run forever"""
asyncio.set_event_loop(loop)
loop.run_forever()
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py:145:RuntimeWarning:协程'get_active_allowed_systems_by_poll'从未等待
解决方案
推荐阅读
- ios - NSFetchedResultsController 'didChange' 使用新的 CollectionDiffing 会导致奇怪的更改操作,从而导致崩溃
- linux - 带有重定向的 linux uniq 命令出错
- java - 如何更改 Java 中 Date 对象的时区偏移量?
- java - 如何在 Java If 语句中使用 OR
- java - 谷歌 OAuth2 获取服务帐户的访问令牌:连接超时
- python - 如何使用lambdify进行导数函数?
- deno - 在 Deno 中读取大型 JSON 文件
- iis - .NET Core 3.0 GA 与 .NET Core 3.0 Preview 8 并行
- json - 如何为接收到的 JSON 格式定义没有 POJOS 的数据模型?
- laravel - 如何从两个单独的表中调用变量