question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trying to run tortoise with quart async rest api

See original GitHub issue

Describe the bug Hi,

I have simple tortoise example that work fine when i create the python event loop and run it. but when i run Quart API server I get strange error, I braked my head trying to figure out what is the issue (I’m running python3.7) The script I ran is below:

import asyncio
from tortoise import Tortoise, fields
from tortoise.models import Model

from quart import Quart, request, abort

app = Quart(__name__)
app.config.from_object(config)

class Users(Model):
    id = fields.IntField(pk=True)
    status = fields.CharField(20)
    def __repr__(self):
        return str(self.id)

class Workers(Model):
    id = fields.IntField(pk=True)
    status = fields.CharField(20)
    def __repr__(self):
        return str(self.id)

@app.before_first_request
async def initial_db():
    await Tortoise.init({
        'connections': {
            'default':  "postgres://localhost:5432/db",
            'second':  "postgres://localhost:5432/db",
        },
        'apps': {
            'users': {
                'models': ['__main__'],
                'default_connection': 'default',
            },
            'workers': {
                'models': ['__main__'],
                'default_connection': 'second',
            }
        }
    })


def run_quart():
    app.run(port=5002)


def run_tortoise():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(initial_db())

if __name__ == '__main__':
    # run_quart() # not working
    run_tortoise() # working

To Reproduce run one time the “run_quart” see the error then run “run_tortoise” see the run succeed

Expected behavior tortoise ORM will use the Quart event loop

Actual behavior Tortoise raise “NotADirectoryError” error

Additional context This is my first time using Tortoise ORM and its looking very cool, I will appreciate any help with this issue

Thanks in advanced!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
shalobacommented, Apr 3, 2019

very strange … cause i just run it using python3.6 and the integration worked! but when comeback to python3.7 the error raised aggain … maybe my virtual env is massed up or something I will create new one and check it again

Thakns @grigi 😃

1reaction
grigicommented, Apr 3, 2019

It ran for me. I used py3.6.6 and Quart==0.6.12 tortoise-orm==0.11.8 asyncpg==0.18.3

So I should try this on Py3.7 I suppose they changed their api. And I see py3.6 is limited to the older version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quart Example — Tortoise ORM v0.17.3 Documentation
This is an example of the Tortoise-ORM Quart integration ... To generate schemas QUART_APP=main quart generate-schemas # To run QUART_APP=main quart run ......
Read more >
python-quart/lobby - Gitter
CAVEAT: Using this solution provides a single database session per request. So the session is used for all synchronous and asynchronous code within...
Read more >
Tutorial: Building a RESTful API — Quart 0.17.0 documentation
In this tutorial we will build a RESTful JSON API. It will automatically generate OpenAPI documentation and validate request and response data. This...
Read more >
What is the right way to initialize database using tortoise orm ...
Consider using an asyncio-based framework, such as Quart/aiohttp/starlette, both that and Tortoise-ORM the uses the same concurrency method and ...
Read more >
Quart; an ASGI alternative to Flask - TIB AV-Portal
Quart is the solution as it shares the Flask API and is based on asyncio. ... although some extensions (Flask-Aiohttp) have tried. Quart...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found