Giter Site home page Giter Site logo

Comments (5)

long2ice avatar long2ice commented on August 23, 2024

看一下代码

from rearq.

zyxbcde avatar zyxbcde commented on August 23, 2024
# main.py
from rearq import ReArq
from tortoise import Tortoise, connections

rearq = ReArq(db_url='mysql://root:[email protected]:3306/rearq')

db_config = {
    "connections": {
        "data": {
            "engine": "tortoise.backends.mysql",
            "credentials": {
                "database": "data",
                "host": "127.0.0.1",
                "password": "123456",
                "port": 3306,
                "user": "root",
            },
        },
        "big_data": {
            "engine": "tortoise.backends.mysql",
            "credentials": {
                "database": "big_data",
                "host": "127.0.0.1",
                "password": "123456",
                "port": 3306,
                "user": "root",
            },
        },
        "default": {
            "engine": "tortoise.backends.mysql",
            "credentials": {
                "database": "rearq",
                "host": "127.0.0.1",
                "password": "123456",
                "port": 3306,
                "user": "root",
            },
        },
    },
    "apps": {
        "data": {"models": [], "default_connection": "data"},
        "big_data": {"models": [], "default_connection": "big_data"},
        "rearq": {"models": ["rearq.server.models"], "default_connection": "default"},
    },
    "timezone": 'Asia/Shanghai'
}


@rearq.on_shutdown
async def on_shutdown():
    # you can do some clean work here like close db and so on...
    connections.close_all()
    print("shutdown")


@rearq.on_startup
async def on_startup():
    # you should do some initialization work here
    await Tortoise.init(config=db_config)
    print("startup")


@rearq.task(queue="q1")
async def add(self, a, b):
    return a + b

#app.py
import uvicorn
from fastapi import FastAPI

from main import rearq
from rearq.server.app import app as rearq_app

app = FastAPI()

app.mount("/rearq", rearq_app)
rearq_app.set_rearq(rearq)

if __name__ == '__main__':
    uvicorn.run(app="app:app", host="0.0.0.0", port=8999)

"""
exception:
  File "/Users/zhouyuxin/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/rearq/server/routes/index.py", line 26, in index
    run_times = await JobResult.all().count()
  File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/models.py", line 1277, in all
    db = using_db or cls._choose_db()
  File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/models.py", line 1033, in _choose_db
    db = router.db_for_read(cls)
  File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/router.py", line 36, in db_for_read
    return self._db_route(model, "db_for_read")
  File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/router.py", line 31, in _db_route
    return connections.get(self._router_func(model, action))
  File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/router.py", line 18, in _router_func
    for r in self._routers:
TypeError: 'NoneType' object is not iterable
"""

from rearq.

long2ice avatar long2ice commented on August 23, 2024

你试试最新的dev代码

from rearq.

zyxbcde avatar zyxbcde commented on August 23, 2024

解决了,打开方式不对,相关提示留给后来人。
一是要注意在fastapi里mount_app时候的执行顺序,需要先register_tortoise
二是注意在数据库的config字典的填写方式,apps里面必须有个app叫rearq,connection的名字可以随便起。
再次感谢。

from rearq.

long2ice avatar long2ice commented on August 23, 2024

好的

from rearq.

Related Issues (14)

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.